knoteslegacy.cpp
00001 /******************************************************************* 00002 KNotes -- Notes for the KDE project 00003 00004 Copyright (c) 2002-2004, Michael Brade <brade@kde.org> 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program 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 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 *******************************************************************/ 00020 00021 #include <tqfile.h> 00022 #include <tqfont.h> 00023 #include <tqpoint.h> 00024 #include <tqcolor.h> 00025 #include <tqstringlist.h> 00026 #include <tqtextstream.h> 00027 00028 #include <kdebug.h> 00029 #include <kapplication.h> 00030 #include <kglobal.h> 00031 #include <kurl.h> 00032 #include <kstandarddirs.h> 00033 #include <ksimpleconfig.h> 00034 #include <kio/netaccess.h> 00035 00036 #include <unistd.h> 00037 00038 #include "knoteslegacy.h" 00039 #include "knoteconfig.h" 00040 #include "version.h" 00041 00042 #include "libkcal/calendarlocal.h" 00043 #include "libkcal/journal.h" 00044 00045 #include <netwm.h> 00046 00047 using namespace KCal; 00048 00049 00050 void KNotesLegacy::cleanUp() 00051 { 00052 // remove old (KDE 1.x) local config file if it still exists 00053 TQString configfile = KGlobal::dirs()->saveLocation( "config" ) + "knotesrc"; 00054 if ( TQFile::exists( configfile ) ) { 00055 KSimpleConfig *test = new KSimpleConfig( configfile ); 00056 test->setGroup( "General" ); 00057 double version = test->readDoubleNumEntry( "version", 1.0 ); 00058 delete test; 00059 00060 if ( version == 1.0 ) { 00061 if ( !( checkAccess( configfile, W_OK ) && 00062 TQFile::remove( configfile ) ) ) 00063 { 00064 kdError(5500) << k_funcinfo << "Could not delete old config file " << configfile << endl; 00065 } 00066 } 00067 } 00068 } 00069 00070 bool KNotesLegacy::convert( CalendarLocal *calendar ) 00071 { 00072 bool converted = false; 00073 00074 TQDir noteDir( KGlobal::dirs()->saveLocation( "appdata", "notes/" ) ); 00075 const TQStringList notes = noteDir.entryList( TQDir::Files, TQDir::Name ); 00076 for ( TQStringList::ConstIterator note = notes.constBegin(); note != notes.constEnd(); ++note ) 00077 { 00078 TQString file = noteDir.absFilePath( *note ); 00079 KSimpleConfig* test = new KSimpleConfig( file ); 00080 test->setGroup( "General" ); 00081 double version = test->readDoubleNumEntry( "version", 1.0 ); 00082 00083 if ( version < 3.0 ) 00084 { 00085 00086 // create the new note 00087 Journal *journal = new Journal(); 00088 bool success; 00089 00090 if ( version < 2.0 ) 00091 success = convertKNotes1Config( journal, noteDir, *note ); 00092 else 00093 success = convertKNotes2Config( journal, noteDir, *note ); 00094 00095 // could not convert file => do not add a new note 00096 if ( !success ) 00097 delete journal; 00098 else 00099 { 00100 calendar->addJournal( journal ); 00101 converted = true; 00102 } 00103 } 00104 // window state changed for version 3.2 00105 else if ( version < 3.2 ) 00106 { 00107 uint state = test->readUnsignedLongNumEntry( "state", NET::SkipTaskbar ); 00108 test->writeEntry( "ShowInTaskbar", (state & NET::SkipTaskbar) ? false : true ); 00109 test->writeEntry( "KeepAbove", (state & NET::KeepAbove) ? true : false ); 00110 test->deleteEntry( "state" ); 00111 } 00112 delete test; 00113 } 00114 00115 return converted; 00116 } 00117 00118 bool KNotesLegacy::convertKNotes1Config( Journal *journal, TQDir& noteDir, 00119 const TQString& file ) 00120 { 00121 TQFile infile( noteDir.absFilePath( file ) ); 00122 if ( !infile.open( IO_ReadOnly ) ) 00123 { 00124 kdError(5500) << k_funcinfo << "Could not open input file: \"" 00125 << infile.name() << "\"" << endl; 00126 return false; 00127 } 00128 00129 TQTextStream input( &infile ); 00130 00131 // get the name 00132 journal->setSummary( input.readLine() ); 00133 00134 TQStringList props = TQStringList::split( '+', input.readLine() ); 00135 00136 // robustness 00137 if ( props.count() != 13 ) 00138 { 00139 kdWarning(5500) << k_funcinfo << "The file \"" << infile.name() 00140 << "\" lacks version information but is not a valid " 00141 << "KNotes 1 config file either!" << endl; 00142 return false; 00143 } 00144 00145 // the new configfile's name 00146 TQString configFile = noteDir.absFilePath( journal->uid() ); 00147 00148 // set the defaults 00149 KIO::NetAccess::copy( 00150 KURL( KGlobal::dirs()->saveLocation( "config" ) + "knotesrc" ), 00151 KURL( configFile ), 00152 0 00153 ); 00154 00155 KNoteConfig config( KSharedConfig::openConfig( configFile, false, false ) ); 00156 config.readConfig(); 00157 config.setVersion( KNOTES_VERSION ); 00158 00159 // get the geometry 00160 config.setWidth( props[3].toUInt() ); 00161 config.setHeight( props[4].toUInt() ); 00162 00163 // get the background color 00164 uint red = input.readLine().toUInt(); 00165 uint green = input.readLine().toUInt(); 00166 uint blue = input.readLine().toUInt(); 00167 config.setBgColor( TQColor( red, green, blue ) ); 00168 00169 // get the foreground color 00170 red = input.readLine().toUInt(); 00171 green = input.readLine().toUInt(); 00172 blue = input.readLine().toUInt(); 00173 config.setFgColor( TQColor( red, green, blue ) ); 00174 00175 // get the font 00176 TQString fontfamily = input.readLine(); 00177 if ( fontfamily.isEmpty() ) 00178 fontfamily = TQString( "Sans Serif" ); 00179 uint size = input.readLine().toUInt(); 00180 size = TQMAX( size, 4 ); 00181 uint weight = input.readLine().toUInt(); 00182 bool italic = ( input.readLine().toUInt() == 1 ); 00183 TQFont font( fontfamily, size, weight, italic ); 00184 00185 config.setTitleFont( font ); 00186 config.setFont( font ); 00187 00188 // 3d frame? Not supported yet! 00189 input.readLine(); 00190 00191 // autoindent 00192 config.setAutoIndent( input.readLine().toUInt() == 1 ); 00193 00194 // KNotes 1 never had rich text 00195 config.setRichText( false ); 00196 00197 int note_desktop = props[0].toUInt(); 00198 00199 // hidden or on all desktops? 00200 if ( input.readLine().toUInt() == 1 ) 00201 note_desktop = 0; 00202 else if ( props[11].toUInt() == 1 ) 00203 note_desktop = NETWinInfo::OnAllDesktops; 00204 00205 config.setDesktop( note_desktop ); 00206 config.setPosition( TQPoint( props[1].toUInt(), props[2].toUInt() ) ); 00207 config.setKeepAbove( props[12].toUInt() & 2048 ); 00208 00209 config.writeConfig(); 00210 00211 // get the text 00212 TQString text; 00213 while ( !input.atEnd() ) 00214 { 00215 text.append( input.readLine() ); 00216 if ( !input.atEnd() ) 00217 text.append( '\n' ); 00218 } 00219 00220 journal->setDescription( text ); 00221 00222 if ( !infile.remove() ) 00223 kdWarning(5500) << k_funcinfo << "Could not delete input file: \"" << infile.name() << "\"" << endl; 00224 00225 return true; 00226 } 00227 00228 bool KNotesLegacy::convertKNotes2Config( Journal *journal, TQDir& noteDir, 00229 const TQString& file ) 00230 { 00231 TQString configFile = noteDir.absFilePath( journal->uid() ); 00232 00233 // new name for config file 00234 if ( !noteDir.rename( file, journal->uid() ) ) 00235 { 00236 kdError(5500) << k_funcinfo << "Could not rename input file: \"" 00237 << noteDir.absFilePath( file ) << "\" to \"" 00238 << configFile << "\"!" << endl; 00239 return false; 00240 } 00241 00242 // update the config 00243 KConfig config( configFile ); 00244 config.setGroup( "Data" ); 00245 journal->setSummary( config.readEntry( "name" ) ); 00246 config.deleteGroup( "Data", true ); 00247 config.setGroup( "General" ); 00248 config.writeEntry( "version", KNOTES_VERSION ); 00249 config.setGroup( "WindowDisplay" ); 00250 uint state = config.readUnsignedLongNumEntry( "state", NET::SkipTaskbar ); 00251 config.writeEntry( "ShowInTaskbar", (state & NET::SkipTaskbar) ? false : true ); 00252 config.writeEntry( "KeepAbove", (state & NET::KeepAbove) ? true : false ); 00253 config.deleteEntry( "state" ); 00254 00255 // load the saved text and put it in the journal 00256 TQFile infile( noteDir.absFilePath( "." + file + "_data" ) ); 00257 if ( infile.open( IO_ReadOnly ) ) 00258 { 00259 TQTextStream input( &infile ); 00260 input.setEncoding( TQTextStream::UnicodeUTF8 ); 00261 journal->setDescription( input.read() ); 00262 if ( !infile.remove() ) 00263 kdWarning(5500) << k_funcinfo << "Could not delete data file: \"" << infile.name() << "\"" << endl; 00264 } 00265 else 00266 kdWarning(5500) << k_funcinfo << "Could not open data file: \"" << infile.name() << "\"" << endl; 00267 00268 return true; 00269 }