KNewStuff
knewstuffgeneric.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <qfile.h>
00023 #include <qtextstream.h>
00024 #include <qdir.h>
00025
00026 #include <kdebug.h>
00027 #include <klocale.h>
00028 #include <kprocess.h>
00029 #include <kconfig.h>
00030 #include <kstandarddirs.h>
00031 #include <kmessagebox.h>
00032 #include <ktar.h>
00033
00034 #include "entry.h"
00035
00036 #include "knewstuffgeneric.h"
00037
00038 using namespace std;
00039
00040 KNewStuffGeneric::KNewStuffGeneric( const QString &type, QWidget *parent )
00041 : KNewStuff( type, parent )
00042 {
00043 mConfig = KGlobal::config();
00044 }
00045
00046 KNewStuffGeneric::~KNewStuffGeneric()
00047 {
00048 }
00049
00050 bool KNewStuffGeneric::install( const QString &fileName )
00051 {
00052 kdDebug() << "KNewStuffGeneric::install(): " << fileName << endl;
00053 QStringList list, list2;
00054
00055 mConfig->setGroup("KNewStuff");
00056
00057 QString uncompress = mConfig->readEntry( "Uncompress" );
00058 if ( !uncompress.isEmpty() ) {
00059 kdDebug() << "Uncompression method: " << uncompress << endl;
00060 KTar tar(fileName, uncompress);
00061 tar.open(IO_ReadOnly);
00062 const KArchiveDirectory *dir = tar.directory();
00063 dir->copyTo(destinationPath(0));
00064 tar.close();
00065 QFile::remove(fileName);
00066 }
00067
00068 QString cmd = mConfig->readEntry( "InstallationCommand" );
00069 if ( !cmd.isEmpty() ) {
00070 kdDebug() << "InstallationCommand: " << cmd << endl;
00071 list = QStringList::split( " ", cmd );
00072 for ( QStringList::iterator it = list.begin(); it != list.end(); ++it ) {
00073 list2 << (*it).replace("%f", fileName);
00074 }
00075 KProcess proc;
00076 proc << list2;
00077 proc.start( KProcess::Block );
00078 }
00079
00080 return true;
00081 }
00082
00083 bool KNewStuffGeneric::createUploadFile( const QString & )
00084 {
00085 return false;
00086 }
00087
00088 QString KNewStuffGeneric::destinationPath( KNS::Entry *entry )
00089 {
00090 QString path, file, target, ext;
00091
00092 mConfig->setGroup("KNewStuff");
00093
00094 if ( entry )
00095 {
00096 ext = entry->payload().fileName().section('.', 1);
00097 if ( ! ext.isEmpty() ) ext = "." + ext;
00098
00099 target = entry->fullName() + ext;
00100 }
00101 else target = "/";
00102 QString res = mConfig->readEntry( "StandardResource" );
00103 if ( res.isEmpty() )
00104 {
00105 target = mConfig->readEntry("TargetDir");
00106 if ( !target.isEmpty())
00107 {
00108 res = "data";
00109 if ( entry ) target.append("/" + entry->fullName() + ext);
00110 else target.append("/");
00111 }
00112 }
00113 if ( res.isEmpty() )
00114 {
00115 path = mConfig->readEntry( "InstallPath" );
00116 }
00117 if ( res.isEmpty() && path.isEmpty() )
00118 {
00119 if ( !entry ) return QString::null;
00120 else return KNewStuff::downloadDestination( entry );
00121 }
00122
00123 if ( !path.isEmpty() )
00124 {
00125 file = QDir::home().path() + "/" + path + "/";
00126 if ( entry ) file += entry->fullName() + ext;
00127 }
00128 else file = locateLocal( res.utf8() , target );
00129
00130 return file;
00131 }
00132
00133 QString KNewStuffGeneric::downloadDestination( KNS::Entry *entry )
00134 {
00135 QString file = destinationPath(entry);
00136
00137 if ( KStandardDirs::exists( file ) ) {
00138 int result = KMessageBox::warningContinueCancel( parentWidget(),
00139 i18n("The file '%1' already exists. Do you want to overwrite it?")
00140 .arg( file ),
00141 QString::null, i18n("Overwrite") );
00142 if ( result == KMessageBox::Cancel ) return QString::null;
00143 }
00144
00145 return file;
00146 }