knewstuffgeneric.cpp
00001 /* 00002 This file is part of KDE. 00003 00004 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include <tqfile.h> 00023 #include <tqtextstream.h> 00024 #include <tqdir.h> 00025 00026 #include <kdebug.h> 00027 #include <tdelocale.h> 00028 #include <kprocess.h> 00029 #include <tdeconfig.h> 00030 #include <kstandarddirs.h> 00031 #include <tdemessagebox.h> 00032 #include <kmimemagic.h> 00033 #include <ktar.h> 00034 00035 #include "entry.h" 00036 00037 #include "knewstuffgeneric.h" 00038 00039 using namespace std; 00040 00041 TDENewStuffGeneric::TDENewStuffGeneric( const TQString &type, TQWidget *parent ) 00042 : TDENewStuff( type, parent ) 00043 { 00044 mConfig = TDEGlobal::config(); 00045 } 00046 00047 TDENewStuffGeneric::~TDENewStuffGeneric() 00048 { 00049 } 00050 00051 bool TDENewStuffGeneric::install( const TQString &fileName ) 00052 { 00053 // Try to detect the most common cases where (usually adware) Web pages are downloaded 00054 // instead of the desired file and abort 00055 KMimeMagicResult *res = KMimeMagic::self()->findFileType( fileName ); 00056 if ( res->isValid() && res->accuracy() > 40 ) { 00057 if (type().lower().contains("wallpaper")) { 00058 if (!res->mimeType().startsWith("image/")) { 00059 TQFile::remove(fileName); 00060 return false; 00061 } 00062 } 00063 } 00064 00065 kdDebug() << "TDENewStuffGeneric::install(): " << fileName << endl; 00066 TQStringList list, list2; 00067 00068 mConfig->setGroup("TDENewStuff"); 00069 00070 TQString uncompress = mConfig->readEntry( "Uncompress" ); 00071 if ( !uncompress.isEmpty() ) { 00072 kdDebug() << "Uncompression method: " << uncompress << endl; 00073 KTar tar(fileName, uncompress); 00074 tar.open(IO_ReadOnly); 00075 const KArchiveDirectory *dir = tar.directory(); 00076 dir->copyTo(destinationPath(0)); 00077 tar.close(); 00078 TQFile::remove(fileName); 00079 } 00080 00081 TQString cmd = mConfig->readEntry( "InstallationCommand" ); 00082 if ( !cmd.isEmpty() ) { 00083 kdDebug() << "InstallationCommand: " << cmd << endl; 00084 list = TQStringList::split( " ", cmd ); 00085 for ( TQStringList::iterator it = list.begin(); it != list.end(); ++it ) { 00086 list2 << (*it).replace("%f", fileName); 00087 } 00088 TDEProcess proc; 00089 proc << list2; 00090 proc.start( TDEProcess::Block ); 00091 } 00092 00093 return true; 00094 } 00095 00096 bool TDENewStuffGeneric::createUploadFile( const TQString & /*fileName*/ ) 00097 { 00098 return false; 00099 } 00100 00101 TQString TDENewStuffGeneric::destinationPath( KNS::Entry *entry ) 00102 { 00103 TQString path, file, target, ext; 00104 00105 mConfig->setGroup("TDENewStuff"); 00106 00107 if ( entry ) 00108 { 00109 ext = entry->payload().fileName().section('.', 1); 00110 if ( ! ext.isEmpty() ) ext = "." + ext; 00111 00112 target = entry->fullName() + ext; 00113 } 00114 else target = "/"; 00115 TQString res = mConfig->readEntry( "StandardResource" ); 00116 if ( res.isEmpty() ) 00117 { 00118 target = mConfig->readEntry("TargetDir"); 00119 if ( !target.isEmpty()) 00120 { 00121 res = "data"; 00122 if ( entry ) target.append("/" + entry->fullName() + ext); 00123 else target.append("/"); 00124 } 00125 } 00126 if ( res.isEmpty() ) 00127 { 00128 path = mConfig->readEntry( "InstallPath" ); 00129 } 00130 if ( res.isEmpty() && path.isEmpty() ) 00131 { 00132 if ( !entry ) return TQString::null; 00133 else return TDENewStuff::downloadDestination( entry ); 00134 } 00135 00136 if ( !path.isEmpty() ) 00137 { 00138 file = TQDir::home().path() + "/" + path + "/"; 00139 if ( entry ) file += entry->fullName() + ext; 00140 } 00141 else file = locateLocal( res.utf8() , target ); 00142 00143 return file; 00144 } 00145 00146 TQString TDENewStuffGeneric::downloadDestination( KNS::Entry *entry ) 00147 { 00148 TQString file = destinationPath(entry); 00149 00150 if ( TDEStandardDirs::exists( file ) ) { 00151 int result = KMessageBox::warningContinueCancel( parentWidget(), 00152 i18n("The file '%1' already exists. Do you want to overwrite it?") 00153 .arg( file ), 00154 TQString::null, i18n("Overwrite") ); 00155 if ( result == KMessageBox::Cancel ) return TQString::null; 00156 } 00157 00158 return file; 00159 }