00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qfile.h>
00022 #include <qfont.h>
00023 #include <qpoint.h>
00024 #include <qcolor.h>
00025 #include <qstringlist.h>
00026 #include <qtextstream.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
00053 QString configfile = KGlobal::dirs()->saveLocation( "config" ) + "knotesrc";
00054 if ( QFile::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 QFile::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 QDir noteDir( KGlobal::dirs()->saveLocation( "appdata", "notes/" ) );
00075 const QStringList notes = noteDir.entryList( QDir::Files, QDir::Name );
00076 for ( QStringList::ConstIterator note = notes.constBegin(); note != notes.constEnd(); ++note )
00077 {
00078 QString 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
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
00096 if ( !success )
00097 delete journal;
00098 else
00099 {
00100 calendar->addJournal( journal );
00101 converted = true;
00102 }
00103 }
00104
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, QDir& noteDir,
00119 const QString& file )
00120 {
00121 QFile 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 QTextStream input( &infile );
00130
00131
00132 journal->setSummary( input.readLine() );
00133
00134 QStringList props = QStringList::split( '+', input.readLine() );
00135
00136
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
00146 QString configFile = noteDir.absFilePath( journal->uid() );
00147
00148
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
00160 config.setWidth( props[3].toUInt() );
00161 config.setHeight( props[4].toUInt() );
00162
00163
00164 uint red = input.readLine().toUInt();
00165 uint green = input.readLine().toUInt();
00166 uint blue = input.readLine().toUInt();
00167 config.setBgColor( QColor( red, green, blue ) );
00168
00169
00170 red = input.readLine().toUInt();
00171 green = input.readLine().toUInt();
00172 blue = input.readLine().toUInt();
00173 config.setFgColor( QColor( red, green, blue ) );
00174
00175
00176 QString fontfamily = input.readLine();
00177 if ( fontfamily.isEmpty() )
00178 fontfamily = QString( "Sans Serif" );
00179 uint size = input.readLine().toUInt();
00180 size = QMAX( size, 4 );
00181 uint weight = input.readLine().toUInt();
00182 bool italic = ( input.readLine().toUInt() == 1 );
00183 QFont font( fontfamily, size, weight, italic );
00184
00185 config.setTitleFont( font );
00186 config.setFont( font );
00187
00188
00189 input.readLine();
00190
00191
00192 config.setAutoIndent( input.readLine().toUInt() == 1 );
00193
00194
00195 config.setRichText( false );
00196
00197 int note_desktop = props[0].toUInt();
00198
00199
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( QPoint( props[1].toUInt(), props[2].toUInt() ) );
00207 config.setKeepAbove( props[12].toUInt() & 2048 );
00208
00209 config.writeConfig();
00210
00211
00212 QString 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, QDir& noteDir,
00229 const QString& file )
00230 {
00231 QString configFile = noteDir.absFilePath( journal->uid() );
00232
00233
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
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
00256 QFile infile( noteDir.absFilePath( "." + file + "_data" ) );
00257 if ( infile.open( IO_ReadOnly ) )
00258 {
00259 QTextStream input( &infile );
00260 input.setEncoding( QTextStream::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 }