-rw-r--r-- | library/applnk.cpp | 83 |
1 files changed, 56 insertions, 27 deletions
diff --git a/library/applnk.cpp b/library/applnk.cpp index 2af6cf4..5185b5f 100644 --- a/library/applnk.cpp +++ b/library/applnk.cpp @@ -15,12 +15,14 @@ ** ** Contact info@trolltech.com if any conditions of this licensing are ** not clear to you. ** **********************************************************************/ +#define QTOPIA_INTERNAL_MIMEEXT + #include "applnk.h" #include <qpe/qpeapplication.h> #include <qpe/categories.h> #include <qpe/categoryselect.h> #include <qpe/qcopenvelope_qws.h> @@ -45,19 +47,29 @@ int AppLnk::lastId = 5000; static int smallSize = 14; static int bigSize = 32; static QString safeFileName(const QString& n) { QString safename=n; - safename.replace(QRegExp("[^0-9A-Za-z.]"),"_"); + safename.replace(QRegExp("[^0-9A-Za-z.]"),"_"); // Njaard says this is broken safename.replace(QRegExp("^[^A-Za-z]*"),""); if ( safename.isEmpty() ) safename = "_"; return safename; } +static bool prepareDirectories(const QString& lf) +{ + if ( !QFile::exists(lf) ) { + // May need to create directories + QFileInfo fi(lf); + if ( system(("mkdir -p "+fi.dirPath(TRUE))) ) + return FALSE; + } + return TRUE; +} class AppLnkPrivate { public: QArray<int> mCat; }; @@ -280,30 +292,40 @@ QString AppLnk::type() const \sa exec() */ QString AppLnk::file() const { if ( mFile.isNull() ) { - AppLnk* that = (AppLnk*)this; + AppLnk* that = (AppLnk*)this; // copy? + QString ext = MimeType(mType).extension(); + if ( !ext.isEmpty() ) + ext = "." + ext; if ( !mLinkFile.isEmpty() ) { that->mFile = mLinkFile.right(8)==".desktop" // 8 = strlen(".desktop") ? mLinkFile.left(mLinkFile.length()-8) : mLinkFile; } else if ( mType.contains('/') ) { that->mFile = QString(getenv("HOME"))+"/Documents/"+mType+"/"+safeFileName(that->mName); - if ( QFile::exists(that->mFile) || QFile::exists(that->mFile+".desktop") ) { + if ( QFile::exists(that->mFile+ext) || QFile::exists(that->mFile+".desktop") ) { // a .desktop with the same name exists int n=1; + qWarning("AppLnk::file() n=1 %s", that->mFile.latin1() ); QString nn; - while (QFile::exists((nn=(that->mFile+"_"+QString::number(n)))) + while (QFile::exists((nn=(that->mFile+"_"+QString::number(n)))+ext) || QFile::exists(nn+".desktop")) n++; that->mFile = nn; + qWarning("AppLnl::file() now mFile is %s", that->mFile.latin1() ); } that->mLinkFile = that->mFile+".desktop"; + that->mFile += ext; } + prepareDirectories(that->mFile); + QFile f(that->mFile); + if ( !f.open(IO_WriteOnly) ) + that->mFile = QString::null; return that->mFile; } return mFile; } /*! @@ -315,30 +337,36 @@ QString AppLnk::linkFile() const { if ( mLinkFile.isNull() ) { AppLnk* that = (AppLnk*)this; if ( type().contains('/') ) { StorageInfo storage; const FileSystem *fs = storage.fileSystemOf( that->mFile ); -// qDebug("creating lnkFile for %s", mFile.latin1() ); -// if ( fs ) -// qDebug("fs is %s", fs->path().latin1() ); if ( fs && fs->isRemovable() ) { -// qDebug("isRemovable"); that->mLinkFile = fs->path(); } else that->mLinkFile = getenv( "HOME" ); that->mLinkFile += "/Documents/"+type()+"/"+safeFileName(that->mName); - if ( QFile::exists(that->mLinkFile+".desktop") ) { + if ( QFile::exists(that->mLinkFile+".desktop") ) { // ok the file exists lets check if we point to the same file int n=1; QString nn; - while (QFile::exists((nn=that->mLinkFile+"_"+QString::number(n))+".desktop")) + AppLnk lnk( that->mLinkFile+".desktop" ); + if(that->file() != lnk.file() ){ + qWarning("AppLnk::linkFile exists %s", that->mLinkFile.latin1() ); + while (QFile::exists((nn=that->mLinkFile+"_"+QString::number(n))+".desktop")){ n++; - that->mLinkFile = nn; + AppLnk lnk(nn ); // just to be sure + if(lnk.file() ==that->file() ){ + break; + } + } + that->mLinkFile = nn; + } } that->mLinkFile += ".desktop"; -// qDebug("file is %s", mLinkFile.latin1() ); + qWarning("AppLnk::linkFile is %s", that->mLinkFile.latin1() ); + storeLink(); } return that->mLinkFile; } return mLinkFile; } @@ -511,19 +539,13 @@ void AppLnk::setCategories( const QArray<int>& c ) Attempts to ensure that the link file for this AppLnk exists, including creating any required directories. Returns TRUE if successful. */ bool AppLnk::ensureLinkExists() const { QString lf = linkFile(); - if ( !QFile::exists(lf) ) { - // May need to create directories - QFileInfo fi(lf); - if ( system(("mkdir -p "+fi.dirPath(TRUE))) ) - return FALSE; - } - return TRUE; + return prepareDirectories(lf); } /*! Commits the AppLnk to disk. Returns whether the operation succeeded. The "linkChanged(QString)" message is sent to the @@ -532,13 +554,19 @@ bool AppLnk::ensureLinkExists() const bool AppLnk::writeLink() const { // Only re-writes settable parts QString lf = linkFile(); if ( !ensureLinkExists() ) return FALSE; - Config config( lf, Config::File ); + storeLink(); + return TRUE; +} + +void AppLnk::storeLink() const +{ + Config config( mLinkFile, Config::File ); config.setGroup("Desktop Entry"); config.writeEntry("Name",mName); if ( !mIconFile.isNull() ) config.writeEntry("Icon",mIconFile); config.writeEntry("Type",type()); if ( !mComment.isNull() ) config.writeEntry("Comment",mComment); config.writeEntry("File",file()); @@ -548,15 +576,13 @@ bool AppLnk::writeLink() const for ( i = 0; i < int(d->mCat.count()); i++ ) { sl.append( QString::number( d->mCat[i] ) ); } config.writeEntry( "Categories", sl, ';' ); QCopEnvelope e("QPE/System", "linkChanged(QString)"); - e << lf; - - return TRUE; + e << mLinkFile; } /*! Sets the property named \a key to \a value. */ void AppLnk::setProperty(const QString& key, const QString& value) @@ -583,29 +609,32 @@ QString AppLnk::property(const QString& key) const /*! Deletes both the linkFile() and file() associated with this AppLnk. */ void AppLnk::removeFiles() { bool valid = isValid(); - if ( !valid || QFile::remove(linkFile()) ) { + if ( !valid || !linkFileKnown() || QFile::remove(linkFile()) ) { if ( QFile::remove(file()) ) { QCopEnvelope e("QPE/System", "linkChanged(QString)"); - e << linkFile(); + if ( linkFileKnown() ) + e << linkFile(); + else + e << file(); } else if ( valid ) { // restore link writeLink(); } } } /*! Delete the linkFile(), leaving any file() untouched. */ void AppLnk::removeLinkFile() { - if ( isValid() && QFile::remove(linkFile()) ) { + if ( isValid() && linkFileKnown() && QFile::remove(linkFile()) ) { QCopEnvelope e("QPE/System", "linkChanged(QString)"); e << linkFile(); } } class AppLnkSetPrivate { @@ -1080,13 +1109,13 @@ QString DocLnk::exec() const void DocLnk::invoke(const QStringList& args) const { MimeType mt(type()); const AppLnk* app = mt.application(); if ( app ) { QStringList a = args; - if ( QFile::exists( linkFile() ) ) + if ( linkFileKnown() && QFile::exists( linkFile() ) ) a.append(linkFile()); else a.append(file()); app->execute(a); } } |