kmobiletools
config.cpp
Go to the documentation of this file.00001 /*************************************************************************** 00002 Copyright (C) 2007 by Matthias Lechner <matthias@lmme.de> 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 ***************************************************************************/ 00019 00020 #include "config.h" 00021 00022 #include <QtCore/QDir> 00023 #include <QtCore/QMutex> 00024 #include <QtCore/QMutexLocker> 00025 00026 #include <KDE/KLocale> 00027 #include <KDE/KTar> 00028 #include <KDE/KMessageBox> 00029 #include <KDE/KStandardDirs> 00030 00031 #include <libkmobiletools/kmobiletools_devices.h> 00032 #include <libkmobiletools/kmobiletools_cfg.h> 00033 00034 namespace KMobileTools { 00035 00036 class ConfigInstance { 00037 public: 00038 Config m_uniqueInstance; 00039 }; 00040 00041 class ConfigPrivate { 00042 public: 00043 const static uint currentVersion = 20070908; 00044 00045 QMutex mutex; 00046 }; 00047 00048 K_GLOBAL_STATIC(ConfigInstance, configInstance) 00049 00050 Config::Config() 00051 : d( new ConfigPrivate ) { 00052 // check if this is the first program start-up 00053 if( !MainConfig::configversion() ) { 00054 KMobileTools::MainConfig::self()->readConfig(); 00055 KMobileTools::MainConfig::self()->setConfigversion( d->currentVersion ); 00056 KMobileTools::MainConfig::self()->writeConfig(); 00057 } 00058 // check if the user's configuration is outdated 00059 else if( d->currentVersion != MainConfig::configversion() ) { 00060 QDir configDir( KGlobal::dirs()->saveLocation("config") ); 00061 QStringList entries = configDir.entryList( QStringList() << "*kmobiletools*", QDir::Files ); 00062 if( !entries.isEmpty() ) { 00063 QString archiveName = KGlobal::dirs()->saveLocation("tmp") + "kmobiletools-" + 00064 QDate::currentDate().toString( Qt::ISODate ) + ".tar.gz"; 00065 00066 KMessageBox::information( 0, i18n("<qt><p>KMobileTools has found an old or invalid " 00067 "configuration file.</p><p>To work correctly, it needs " 00068 "to delete your configuration files. Your old files will " 00069 "be saved in <b>%1</b></p></qt>", archiveName ) ); 00070 KTar arch( archiveName ); 00071 if( !arch.open( QIODevice::WriteOnly) ) { 00072 KMessageBox::error( 0, i18n("<qt><p>KMobileTools could not archive your config files.</p><p>Please remove them manually.</p></qt>") ); 00073 } else { 00074 for( QStringList::Iterator it=entries.begin(); it!=entries.end(); ++it ) { 00075 arch.addLocalFile( configDir.path() + QDir::separator() + (*it), (*it)); 00076 QFile::remove( configDir.path() + QDir::separator() + (*it) ); 00077 } 00078 arch.close(); 00079 00080 KMobileTools::MainConfig::self()->readConfig(); 00081 KMobileTools::MainConfig::self()->setConfigversion( d->currentVersion ); 00082 KMobileTools::MainConfig::self()->writeConfig(); 00083 } 00084 } 00085 } 00086 } 00087 00088 Config::~Config() 00089 { 00090 delete d; 00091 } 00092 00093 Config* Config::instance() { 00094 // instance is automatically created 00095 return &configInstance->m_uniqueInstance; 00096 } 00097 00098 void Config::addDevice( const QString& name, const QString& engine ) { 00099 QMutexLocker( &d->mutex ); 00100 00101 QStringList deviceList = MainConfig::devicelist(); 00102 if( !deviceList.contains( name ) ) { 00103 deviceList.append( name ); 00104 00105 // write configuration entries 00106 MainConfig::self()->readConfig(); 00107 MainConfig::self()->setDevicelist( deviceList ); 00108 MainConfig::self()->writeConfig(); 00109 00110 DevicesConfigBase devicesConfig( name ); 00111 devicesConfig.readConfig(); 00112 devicesConfig.setEngine( engine ); 00113 devicesConfig.writeConfig(); 00114 } else { 00116 ; 00117 } 00118 } 00119 00120 void Config::removeDevice( const QString& name ) { 00121 QMutexLocker( &d->mutex ); 00122 00123 QStringList deviceList = MainConfig::devicelist(); 00124 if( deviceList.contains( name ) ) { 00125 // retrieve KConfig object from devices config 00126 DevicesConfigBase devicesConfig( name ); 00127 KConfig* config = devicesConfig.config(); 00128 00129 // take the device from the device list 00130 deviceList.removeAll( name ); 00131 00132 // write config entries 00133 config->deleteGroup( name ); 00134 MainConfig::self()->readConfig(); 00135 MainConfig::self()->setDevicelist( deviceList ); 00136 MainConfig::self()->writeConfig(); 00137 } else { 00139 ; 00140 } 00141 } 00142 00143 QStringList Config::deviceList() const { 00144 return MainConfig::devicelist(); 00145 } 00146 00147 QString Config::engine( const QString& deviceName ) { 00148 DevicesConfigBase deviceConfig( deviceName ); 00149 return deviceConfig.engine(); 00150 } 00151 00152 }
KDE 4.2 API Reference