00001
00002
00003 #include <config.h>
00004 #include <qdir.h>
00005
00006 #include "kmfolderdir.h"
00007 #include "kmfoldersearch.h"
00008 #include "kmfoldercachedimap.h"
00009 #include "kmfolder.h"
00010
00011 #include <assert.h>
00012 #include <errno.h>
00013 #include <klocale.h>
00014 #include <kmessagebox.h>
00015 #include <kdebug.h>
00016 #include <kstandarddirs.h>
00017
00018
00019
00020
00021 KMFolderRootDir::KMFolderRootDir(KMFolderMgr* manager, const QString& path,
00022 KMFolderDirType dirType)
00023 : KMFolderDir( 0, 0, path, dirType ),
00024 mPath( path ),
00025 mManager( manager )
00026 {
00027 }
00028
00029
00030 KMFolderRootDir::~KMFolderRootDir()
00031 {
00032
00033
00034
00035 clear();
00036 }
00037
00038
00039 void KMFolderRootDir::setPath(const QString& aPath)
00040 {
00041 mPath = aPath;
00042 }
00043
00044
00045
00046 QString KMFolderRootDir::path() const
00047 {
00048 return mPath;
00049 }
00050
00051
00052
00053 QString KMFolderRootDir::prettyURL() const
00054 {
00055 if ( !mBaseURL.isEmpty() )
00056 return i18n( mBaseURL.data() );
00057 else
00058 return QString::null;
00059 }
00060
00061
00062
00063 void KMFolderRootDir::setBaseURL( const QCString &baseURL )
00064 {
00065 mBaseURL = baseURL;
00066 }
00067
00068
00069
00070 KMFolderMgr* KMFolderRootDir::manager() const
00071 {
00072 return mManager;
00073 }
00074
00075
00076
00077
00078 KMFolderDir::KMFolderDir( KMFolder * owner, KMFolderDir* parent,
00079 const QString& name, KMFolderDirType dirType )
00080 : KMFolderNode( parent, name ), KMFolderNodeList(),
00081 mOwner( owner ), mDirType( dirType )
00082 {
00083 setAutoDelete( true );
00084 }
00085
00086
00087
00088 KMFolderDir::~KMFolderDir()
00089 {
00090 clear();
00091 }
00092
00093
00094
00095 KMFolder* KMFolderDir::createFolder(const QString& aFolderName, bool aSysFldr, KMFolderType aFolderType)
00096 {
00097 KMFolder* fld;
00098
00099 assert(!aFolderName.isEmpty());
00100
00101 if (mDirType == KMImapDir)
00102 fld = new KMFolder( this, aFolderName, KMFolderTypeImap );
00103 else
00104 fld = new KMFolder( this, aFolderName, aFolderType );
00105
00106 assert(fld != 0);
00107 fld->setSystemFolder(aSysFldr);
00108
00109 KMFolderNode* fNode;
00110 int index = 0;
00111 for (fNode=first(); fNode; fNode=next()) {
00112 if (fNode->name().lower() > fld->name().lower()) {
00113 insert( index, fld );
00114 break;
00115 }
00116 ++index;
00117 }
00118
00119 if (!fNode)
00120 append(fld);
00121
00122 fld->correctUnreadMsgsCount();
00123 return fld;
00124 }
00125
00126
00127
00128 QString KMFolderDir::path() const
00129 {
00130 static QString p;
00131
00132 if (parent())
00133 {
00134 p = parent()->path();
00135 p.append("/");
00136 p.append(name());
00137 }
00138 else p = "";
00139
00140 return p;
00141 }
00142
00143
00144
00145 QString KMFolderDir::label() const
00146 {
00147 if ( mOwner )
00148 return mOwner->label();
00149 else
00150 return QString::null;
00151 }
00152
00153
00154
00155 QString KMFolderDir::prettyURL() const
00156 {
00157 QString parentUrl;
00158 if ( parent() )
00159 parentUrl = parent()->prettyURL();
00160 if ( !parentUrl.isEmpty() )
00161 return parentUrl + '/' + label();
00162 else
00163 return label();
00164 }
00165
00166
00167
00168 bool KMFolderDir::reload(void)
00169 {
00170 QDir dir;
00171 KMFolder* folder;
00172 QFileInfo* fileInfo;
00173 QStringList diList;
00174 QPtrList<KMFolder> folderList;
00175
00176 clear();
00177
00178 const QString fldPath = path();
00179 dir.setFilter(QDir::Files | QDir::Dirs | QDir::Hidden);
00180 dir.setNameFilter("*");
00181
00182 if (!dir.cd(fldPath, TRUE))
00183 {
00184 QString msg = i18n("<qt>Cannot enter folder <b>%1</b>.</qt>").arg(fldPath);
00185 KMessageBox::information(0, msg);
00186 return FALSE;
00187 }
00188
00189 QFileInfoList* fiList=(QFileInfoList*)dir.entryInfoList();
00190 if (!fiList)
00191 {
00192 QString msg = i18n("<qt>Folder <b>%1</b> is unreadable.</qt>").arg(fldPath);
00193 KMessageBox::information(0, msg);
00194 return FALSE;
00195 }
00196
00197 for (fileInfo=fiList->first(); fileInfo; fileInfo=fiList->next())
00198 {
00199 const QString fname = fileInfo->fileName();
00200 if( ( fname[0] == '.' ) && !fname.endsWith( ".directory" ) ) {
00201
00202 continue;
00203 }
00204 if( fname == ".directory" ) {
00205
00206 continue;
00207 }
00208
00209 if ( fileInfo->isDir() &&
00210 fname.startsWith( "." ) && fname.endsWith( ".directory" ) ) {
00211 diList.append(fname);
00212 continue;
00213 }
00214
00215 if ( mDirType == KMImapDir
00216 && path().startsWith( KMFolderImap::cacheLocation() ) )
00217 {
00218
00219 if ( KMFolderImap::encodeFileName(
00220 KMFolderImap::decodeFileName( fname ) ) == fname )
00221 {
00222 folder = new KMFolder( this, KMFolderImap::decodeFileName( fname ),
00223 KMFolderTypeImap );
00224 append(folder);
00225 folderList.append(folder);
00226 }
00227 }
00228 else if ( mDirType == KMDImapDir
00229 && path().startsWith( KMFolderCachedImap::cacheLocation() ) )
00230 {
00231 if (fileInfo->isDir())
00232 {
00233
00234
00235 QString maildir(fname + "/new");
00236 QString imapcachefile = QString::fromLatin1(".%1.uidcache").arg(fname);
00237 if ( dir.exists( imapcachefile) || dir.exists( maildir ) )
00238 {
00239 folder = new KMFolder( this, fname, KMFolderTypeCachedImap );
00240 append(folder);
00241 folderList.append(folder);
00242 }
00243 }
00244 }
00245 else if ( mDirType == KMSearchDir)
00246 {
00247 folder = new KMFolder( this, fname, KMFolderTypeSearch );
00248 append(folder);
00249 folderList.append(folder);
00250 }
00251 else if ( mDirType == KMStandardDir )
00252 {
00253
00254
00255 if (fileInfo->isDir())
00256 {
00257
00258 if( dir.exists( fname + "/new" ) )
00259 {
00260 folder = new KMFolder( this, fname, KMFolderTypeMaildir );
00261 append(folder);
00262 folderList.append(folder);
00263 }
00264 }
00265 else
00266 {
00267
00268 folder = new KMFolder( this, fname, KMFolderTypeMbox );
00269 append(folder);
00270 folderList.append(folder);
00271 }
00272 }
00273 }
00274
00275 for (folder=folderList.first(); folder; folder=folderList.next())
00276 {
00277 for(QStringList::Iterator it = diList.begin();
00278 it != diList.end();
00279 ++it)
00280 if (*it == "." + folder->fileName() + ".directory")
00281 {
00282 KMFolderDir* folderDir = new KMFolderDir( folder, this, *it, mDirType);
00283 folderDir->reload();
00284 append(folderDir);
00285 folder->setChild(folderDir);
00286 break;
00287 }
00288 }
00289 return TRUE;
00290 }
00291
00292
00293
00294 KMFolderNode* KMFolderDir::hasNamedFolder(const QString& aName)
00295 {
00296 KMFolderNode* fNode;
00297 for (fNode=first(); fNode; fNode=next()) {
00298 if (fNode->name() == aName)
00299 return fNode;
00300 }
00301 return 0;
00302 }
00303
00304
00305
00306 KMFolderMgr* KMFolderDir::manager() const
00307 {
00308 return parent()->manager();
00309 }
00310
00311
00312 #include "kmfolderdir.moc"
00313