OSDN Git Service

add Kita::Cache
authorikemo <ikemo@56b19765-1e22-0410-a548-a0f45d66c51a>
Tue, 10 Feb 2004 16:24:09 +0000 (16:24 +0000)
committerikemo <ikemo@56b19765-1e22-0410-a548-a0f45d66c51a>
Tue, 10 Feb 2004 16:24:09 +0000 (16:24 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/kita/kita/trunk@865 56b19765-1e22-0410-a548-a0f45d66c51a

kita/src/libkita/Makefile.am
kita/src/libkita/access.cpp
kita/src/libkita/access.h
kita/src/libkita/cache.cpp [new file with mode: 0644]
kita/src/libkita/cache.h [new file with mode: 0644]

index e2858ce..76de49d 100644 (file)
@@ -2,9 +2,9 @@ INCLUDES = $(all_includes)
 
 lib_LTLIBRARIES = libkita.la
 
-libkita_la_SOURCES = comment.cpp comment.h thread.h thread.cpp qcp932codec.cpp qcp932codec.h board.h board.cpp bbs.h bbs.cpp category.h category.cpp favoritethreads.h favoritethreads.cpp k2ch_articlefile.h k2ch_articlefile.cpp kita_misc.cpp threadinfo.h threadinfo.cpp access.cpp
+libkita_la_SOURCES = comment.cpp comment.h thread.h thread.cpp qcp932codec.cpp qcp932codec.h board.h board.cpp bbs.h bbs.cpp category.h category.cpp favoritethreads.h favoritethreads.cpp k2ch_articlefile.h k2ch_articlefile.cpp kita_misc.cpp threadinfo.h threadinfo.cpp access.cpp cache.cpp
 
 METASOURCES = AUTO
 
 SUBDIRS =  tests
-noinst_HEADERS = kita_misc.h access.h
+noinst_HEADERS = kita_misc.h access.h cache.h
index d265fda..8c3d45f 100644 (file)
@@ -11,6 +11,7 @@
 
 #include "thread.h"
 #include "qcp932codec.h"
+#include "cache.h"
 
 #include <config.h>
 
 
 using namespace Kita;
 
-QString Access::cacheDir( const KURL& url )
-{
-    QString dir;
-    if ( url.protocol() == "k2ch" ) {
-        dir = KGlobal::dirs() ->saveLocation( "cache", "k2ch" );
-    } else {
-        dir = KProtocolManager::cacheDir();
-    }
-    if ( dir[ dir.length() - 1 ] != '/' )
-        dir += "/";
-
-    return dir;
-}
-
-QString Access::cacheSubDir( const QString& host )
-{
-    int l = host.length();
-    for ( int i = 0; i < l; i++ ) {
-        if ( host[ i ].isLetter() && ( host[ i ] != 'w' ) ) {
-            return QString( host[ i ] );
-        }
-    }
-    return "0";
-}
-
-QString Access::hashString( const KURL& url )
-{
-    unsigned long hash = 0x00000000;
-    QCString u = url.url().latin1();
-    for ( int i = u.length(); i--; ) {
-        hash = ( hash * 12211 + u[ i ] ) % 2147483563;
-    }
-
-    QString ret;
-    ret.sprintf( "%08lx", hash );
-
-    return ret;
-}
-
-// copied from http.cc
-QString Access::getCachePath( const KURL& url )
-{
-    QString path = url.path();
-    path.replace( QChar( '/' ), QChar( '_' ) );
-
-    QString host = url.host().lower();
-
-    return cacheDir( url ) + cacheSubDir( host ) + "/" + host + path + '_' + hashString( url );
-}
-
 bool Access::readCacheHeader( FILE* fs, const KURL& url )
 {
     QFile file;
@@ -150,7 +101,7 @@ bool Access::readCacheHeader( FILE* fs, const KURL& url )
 // ¥­¥ã¥Ã¥·¥å¤¬¤Ê¤¤¤È¤­¤Ï0¤òÊÖ¤¹¡£
 QCString Access::getCacheData( const KURL& url )
 {
-    QString cachePath = getCachePath( url );
+    QString cachePath = Kita::Cache::getPath( url );
 
     FILE *fs = fopen( QFile::encodeName( cachePath ), "r" );
     if ( !fs ) {
@@ -179,7 +130,7 @@ QCString Access::getCacheData( const KURL& url )
 // partial data¤¬Æþ¤Ã¤Æ¤ë¤Î¤Ç¤½¤ì¤ò½ñ¤­´¹¤¨¤ë¡£
 void Access::writeCacheData( const KURL& url )
 {
-    QString cachePath = getCachePath( url );
+    QString cachePath = Kita::Cache::getPath( url );
 
     FILE *fs = fopen( QFile::encodeName( cachePath ), "r+" );
     if ( !fs ) {
@@ -211,7 +162,7 @@ QString Access::get()
     m_orgData = getCacheData( m_thread->datURL() );
 
     // copy original cache file
-    QString cachePath = getCachePath( m_thread->datURL() );
+    QString cachePath = Kita::Cache::getPath( m_thread->datURL() );
     QString backupPath = cachePath + ".kita_backup";
     KIO::NetAccess::copy( cachePath, backupPath );
 
@@ -262,7 +213,7 @@ void Access::enter_loop()
 void Access::slotRedirection( KIO::Job *, const KURL & newURL )
 {
     // datÍî¤Á¤·¤Æ¤ë¤È»×¤ï¤ì¤ë¤Î¤Ç¥­¥ã¥Ã¥·¥å¤ò¸µ¤ËÌ᤹
-    QString cachePath = getCachePath( m_thread->datURL() );
+    QString cachePath = Kita::Cache::getPath( m_thread->datURL() );
     QString backupPath = cachePath + ".kita_backup";
     KIO::NetAccess::copy( backupPath, cachePath );
     KIO::NetAccess::del( backupPath );
index b47ae6b..334e98f 100644 (file)
@@ -40,14 +40,10 @@ namespace Kita
 
     private:
         void enter_loop();
-        QString getCachePath( const KURL& url );
         QCString getCacheData( const KURL& url );
         void writeCacheData( const KURL& url );
         int responseCode();
         bool readCacheHeader( FILE* fs, const KURL& url );
-        QString cacheDir( const KURL& url );
-        QString cacheSubDir( const QString& host );
-        QString hashString( const KURL& url );
 
         const Thread* m_thread;
         KIO::Job* m_currentJob;
diff --git a/kita/src/libkita/cache.cpp b/kita/src/libkita/cache.cpp
new file mode 100644 (file)
index 0000000..91ecd71
--- /dev/null
@@ -0,0 +1,67 @@
+/***************************************************************************
+*   Copyright (C) 2003 by Hideki Ikemoto                                  *
+*   ikemo@wakaba.jp                                                       *
+*                                                                         *
+*   This program is free software; you can redistribute it and/or modify  *
+*   it under the terms of the GNU General Public License as published by  *
+*   the Free Software Foundation; either version 2 of the License, or     *
+*   (at your option) any later version.                                   *
+***************************************************************************/
+#include "cache.h"
+
+#include <kurl.h>
+#include <kglobal.h>
+#include <kstandarddirs.h>
+#include <kprotocolmanager.h>
+
+using namespace Kita;
+
+QString Cache::baseDir( const KURL& url )
+{
+    QString dir;
+    if ( url.protocol() == "k2ch" ) {
+        dir = KGlobal::dirs() ->saveLocation( "cache", "k2ch" );
+    } else {
+        dir = KProtocolManager::cacheDir();
+    }
+    if ( dir[ dir.length() - 1 ] != '/' )
+        dir += "/";
+
+    return dir;
+}
+
+QString Cache::subDir( const QString& host )
+{
+    int l = host.length();
+    for ( int i = 0; i < l; i++ ) {
+        if ( host[ i ].isLetter() && ( host[ i ] != 'w' ) ) {
+            return QString( host[ i ] );
+        }
+    }
+    return "0";
+}
+
+QString Cache::hashString( const KURL& url )
+{
+    unsigned long hash = 0x00000000;
+    QCString u = url.url().latin1();
+    for ( int i = u.length(); i--; ) {
+        hash = ( hash * 12211 + u[ i ] ) % 2147483563;
+    }
+
+    QString ret;
+    ret.sprintf( "%08lx", hash );
+
+    return ret;
+}
+
+// original is in http.cc
+QString Cache::getPath( const KURL& url )
+{
+    QString path = url.path();
+    path.replace( QChar( '/' ), QChar( '_' ) );
+
+    QString host = url.host().lower();
+
+    return baseDir( url ) + subDir( host ) + "/" + host + path + '_' + hashString( url );
+}
diff --git a/kita/src/libkita/cache.h b/kita/src/libkita/cache.h
new file mode 100644 (file)
index 0000000..1a7752e
--- /dev/null
@@ -0,0 +1,34 @@
+/***************************************************************************
+*   Copyright (C) 2003 by Hideki Ikemoto                                  *
+*   ikemo@wakaba.jp                                                       *
+*                                                                         *
+*   This program is free software; you can redistribute it and/or modify  *
+*   it under the terms of the GNU General Public License as published by  *
+*   the Free Software Foundation; either version 2 of the License, or     *
+*   (at your option) any later version.                                   *
+***************************************************************************/
+#ifndef KITACACHE_H
+#define KITACACHE_H
+
+#include <qstring.h>
+
+class KURL;
+
+namespace Kita
+{
+
+    /**
+    @author Hideki Ikemoto
+    */
+    class Cache
+    {
+        static QString baseDir( const KURL& url );
+        static QString subDir( const QString& host );
+        static QString hashString( const KURL& url );
+    public:
+        static QString getPath( const KURL& url );
+    };
+
+};
+
+#endif