kpilot

fileInstaller.cc

Go to the documentation of this file.
00001 /* KPilot
00002 **
00003 ** Copyright (C) 1998-2001 by Dan Pilone
00004 **
00005 ** This is a class that does "the work" of adding and deleting
00006 ** files in the pending_install directory of KPilot. It is used
00007 ** by the fileInstallWidget and by the daemon's drag-and-drop
00008 ** file accepter.
00009 */
00010 
00011 /*
00012 ** This program is free software; you can redistribute it and/or modify
00013 ** it under the terms of the GNU General Public License as published by
00014 ** the Free Software Foundation; either version 2 of the License, or
00015 ** (at your option) any later version.
00016 **
00017 ** This program is distributed in the hope that it will be useful,
00018 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00020 ** GNU General Public License for more details.
00021 **
00022 ** You should have received a copy of the GNU General Public License
00023 ** along with this program in a file called COPYING; if not, write to
00024 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 ** MA 02110-1301, USA.
00026 */
00027 
00028 /*
00029 ** Bug reports and questions can be sent to kde-pim@kde.org
00030 */
00031 
00032 
00033 #include "options.h"
00034 
00035 #include <unistd.h>
00036 
00037 #include <qstring.h>
00038 #include <qstrlist.h>
00039 #include <qdir.h>
00040 
00041 #include <kglobal.h>
00042 #include <kstandarddirs.h>
00043 #include <kurl.h>
00044 #include <kio/netaccess.h>
00045 #include <kmessagebox.h>
00046 
00047 #include "fileInstaller.moc"
00048 
00049 FileInstaller::FileInstaller() :
00050     enabled(true)
00051 {
00052     FUNCTIONSETUP;
00053 
00054     fDirName = KGlobal::dirs()->saveLocation("data",
00055         CSL1("kpilot/pending_install/"));
00056     fPendingCopies = 0;
00057 
00058 }
00059 
00060 /* virtual */ FileInstaller::~FileInstaller()
00061 {
00062     FUNCTIONSETUP;
00063 }
00064 
00065 
00066 void FileInstaller::clearPending()
00067 {
00068     FUNCTIONSETUP;
00069 
00070     unsigned int i;
00071 
00072     QDir installDir(fDirName);
00073 
00074     // Start from 2 to skip . and ..
00075     //
00076     for (i = 2; i < installDir.count(); i++)
00077     {
00078         QFile::remove(fDirName + installDir[i]);
00079     }
00080 
00081     if (i > 2)
00082     {
00083         emit filesChanged();
00084     }
00085 }
00086 
00087 void FileInstaller::deleteFile(const QString &file)
00088 {
00089     QFile::remove(fDirName + file);
00090     emit filesChanged();
00091 }
00092 
00093 void FileInstaller::deleteFiles(const QStringList &files)
00094 {
00095     if(files.empty())
00096         return;
00097 
00098     for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
00099         QFile::remove(fDirName + *it);
00100     
00101     emit filesChanged();
00102 }
00103 
00104 /* virtual */ bool FileInstaller::runCopy(const QString & s, QWidget* w )
00105 {
00106     FUNCTIONSETUP;
00107 
00108     if(!(s.endsWith(CSL1(".pdb"), false) || s.endsWith(CSL1(".prc"), false))) {
00109         KMessageBox::detailedSorry(w, i18n("Cannot install %1").arg(s),
00110             i18n("Only PalmOS database files (like *.pdb and *.prc) can be installed by the file installer."));
00111         return false;
00112     }
00113 
00114 #ifdef DEBUG
00115     DEBUGKPILOT << fname << ": Copying " << s << endl;
00116 #endif
00117 
00118     KURL srcName;
00119     srcName.setPath(s);
00120     KURL destDir(fDirName + CSL1("/") + srcName.fileName());
00121 
00122 #if KDE_IS_VERSION(3,1,9)
00123     return KIO::NetAccess::copy(srcName, destDir, w);
00124 #else
00125     return KIO::NetAccess::copy(srcName,destDir);
00126 #endif
00127 }
00128 
00129 
00130 void FileInstaller::addFiles(const QStringList & fileList, QWidget* w)
00131 {
00132     FUNCTIONSETUP;
00133 
00134     if (!enabled) return;
00135 
00136     unsigned int succ = 0;
00137 
00138     for(QStringList::ConstIterator it = fileList.begin();
00139         it != fileList.end(); ++it)
00140     {
00141         if (runCopy( *it, w ))
00142             succ++;
00143     }
00144 
00145     if (succ)
00146     {
00147         emit filesChanged();
00148     }
00149 }
00150 
00151 void FileInstaller::addFile( const QString & file, QWidget* w )
00152 {
00153     FUNCTIONSETUP;
00154 
00155     if (!enabled) return;
00156 
00157     if (runCopy(file, w))
00158     {
00159         emit(filesChanged());
00160     }
00161 }
00162 
00163 /* slot */ void FileInstaller::copyCompleted()
00164 {
00165     FUNCTIONSETUP;
00166 }
00167 
00168 const QStringList FileInstaller::fileNames() const
00169 {
00170     FUNCTIONSETUP;
00171 
00172     QDir installDir(fDirName);
00173 
00174     return installDir.entryList(QDir::Files |
00175         QDir::NoSymLinks | QDir::Readable);
00176 }
00177 
00178 /* slot */ void FileInstaller::setEnabled(bool b)
00179 {
00180     FUNCTIONSETUP;
00181     enabled=b;
00182 }
00183 
00184