knotes
knoteslegacy.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 #include <QFile>
00022 #include <QFont>
00023 #include <QPoint>
00024 #include <QColor>
00025 #include <QStringList>
00026 #include <QTextStream>
00027
00028 #include <kdebug.h>
00029 #include <kapplication.h>
00030 #include <kglobal.h>
00031 #include <kurl.h>
00032 #include <kstandarddirs.h>
00033 #include <kconfig.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 <kcal/calendarlocal.h>
00043 #include <kcal/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 KConfigGroup test(
00056 KSharedConfig::openConfig( configfile, KConfig::SimpleConfig ),
00057 "General" );
00058 double version = test.readEntry( "version", 1.0 );
00059
00060 if ( version == 1.0 ) {
00061 if ( !( KStandardDirs::checkAccess( configfile, W_OK ) &&
00062 QFile::remove( configfile ) ) ) {
00063 kError( 5500 ) <<"Could not delete old config file" << configfile;
00064 }
00065 }
00066 }
00067 }
00068
00069 bool KNotesLegacy::convert( CalendarLocal *calendar )
00070 {
00071 bool converted = false;
00072
00073 QDir noteDir( KGlobal::dirs()->saveLocation( "appdata", "notes/" ) );
00074 QStringList notes = noteDir.entryList( QDir::Files, QDir::Name );
00075 for ( QStringList::Iterator note = notes.begin(); note != notes.end();
00076 note++ ) {
00077 QString file = noteDir.absoluteFilePath( *note );
00078 KConfig *test = new KConfig( file, KConfig::SimpleConfig );
00079 KConfigGroup grp( test, "General" );
00080 double version = grp.readEntry( "version", 1.0 );
00081
00082 if ( version < 3.0 ) {
00083 delete test;
00084
00085
00086 Journal *journal = new Journal();
00087 bool success;
00088
00089 if ( version < 2.0 ) {
00090 success = convertKNotes1Config( journal, noteDir, *note );
00091 } else {
00092 success = convertKNotes2Config( journal, noteDir, *note );
00093 }
00094
00095
00096 if ( !success ) {
00097 delete journal;
00098 } else {
00099 calendar->addJournal( journal );
00100 converted = true;
00101 }
00102 } else if ( version < 3.2 ) {
00103 #ifdef Q_WS_X11
00104 uint state = grp.readEntry( "state", uint( NET::SkipTaskbar ) );
00105
00106 grp.writeEntry( "ShowInTaskbar",
00107 ( state & NET::SkipTaskbar ) ? false : true );
00108 grp.writeEntry( "KeepAbove",
00109 ( state & NET::KeepAbove ) ? true : false );
00110 #endif
00111 grp.deleteEntry( "state" );
00112 delete test;
00113 } else {
00114 delete test;
00115 }
00116
00117 }
00118
00119 return converted;
00120 }
00121
00122 bool KNotesLegacy::convertKNotes1Config( Journal *journal, QDir ¬eDir,
00123 const QString &file )
00124 {
00125 QFile infile( noteDir.absoluteFilePath( file ) );
00126 if ( !infile.open( QIODevice::ReadOnly ) ) {
00127 kError( 5500 ) << "Could not open input file: \""
00128 << infile.fileName() << "\"";
00129 return false;
00130 }
00131
00132 QTextStream input( &infile );
00133
00134
00135 journal->setSummary( input.readLine() );
00136
00137 QStringList props = input.readLine().split( '+', QString::SkipEmptyParts );
00138
00139
00140 if ( props.count() != 13 ) {
00141 kWarning( 5500 ) <<"The file \"" << infile.fileName()
00142 << "\" lacks version information but is not a valid"
00143 << "KNotes 1 config file either!";
00144 return false;
00145 }
00146
00147
00148 QString configFile = noteDir.absoluteFilePath( journal->uid() );
00149
00150
00151 KIO::NetAccess::file_copy(
00152 KUrl( KGlobal::dirs()->saveLocation( "config" ) + "knotesrc" ),
00153 KUrl( configFile ),
00154 0
00155 );
00156
00157 KNoteConfig config( KSharedConfig::openConfig( configFile,
00158 KConfig::NoGlobals ) );
00159 config.readConfig();
00160 config.setVersion( KNOTES_VERSION );
00161
00162
00163 config.setWidth( props[3].toUInt() );
00164 config.setHeight( props[4].toUInt() );
00165
00166
00167 uint red = input.readLine().toUInt();
00168 uint green = input.readLine().toUInt();
00169 uint blue = input.readLine().toUInt();
00170 config.setBgColor( QColor( Qt::red, Qt::green, Qt::blue ) );
00171
00172
00173 red = input.readLine().toUInt();
00174 green = input.readLine().toUInt();
00175 blue = input.readLine().toUInt();
00176 config.setFgColor( QColor( Qt::red, Qt::green, Qt::blue ) );
00177
00178
00179 QString fontfamily = input.readLine();
00180 if ( fontfamily.isEmpty() )
00181 fontfamily = QString( "Sans Serif" );
00182 uint size = input.readLine().toUInt();
00183 size = qMax( size, ( uint ) 4 );
00184 uint weight = input.readLine().toUInt();
00185 bool italic = ( input.readLine().toUInt() == 1 );
00186 QFont font( fontfamily, size, weight, italic );
00187
00188 config.setTitleFont( font );
00189 config.setFont( font );
00190
00191
00192 input.readLine();
00193
00194
00195 config.setAutoIndent( input.readLine().toUInt() == 1 );
00196
00197
00198 config.setRichText( false );
00199
00200 int note_desktop = props[0].toUInt();
00201
00202
00203 if ( input.readLine().toUInt() == 1 )
00204 note_desktop = 0;
00205 #ifdef Q_WS_X11
00206 else if ( props[11].toUInt() == 1 )
00207 note_desktop = NETWinInfo::OnAllDesktops;
00208 #endif
00209
00210 config.setDesktop( note_desktop );
00211 config.setPosition( QPoint( props[1].toUInt(), props[2].toUInt() ) );
00212 config.setKeepAbove( props[12].toUInt() & 2048 );
00213
00214 config.writeConfig();
00215
00216
00217 QString text;
00218 while ( !input.atEnd() ) {
00219 text.append( input.readLine() );
00220 if ( !input.atEnd() ) {
00221 text.append( '\n' );
00222 }
00223 }
00224
00225 journal->setDescription( text );
00226
00227 if ( !infile.remove() )
00228 {
00229 kWarning( 5500 ) << "Could not delete input file: \"" << infile.fileName()
00230 << "\"";
00231 }
00232
00233 return true;
00234 }
00235
00236 bool KNotesLegacy::convertKNotes2Config( Journal *journal, QDir ¬eDir,
00237 const QString &file )
00238 {
00239 QString configFile = noteDir.absoluteFilePath( journal->uid() );
00240
00241
00242 if ( !noteDir.rename( file, journal->uid() ) ) {
00243 kError( 5500 ) << "Could not rename input file: \""
00244 << noteDir.absoluteFilePath( file ) << "\" to \""
00245 << configFile << "\"!";
00246 return false;
00247 }
00248
00249
00250 KConfig config( configFile );
00251 KConfigGroup grp( &config, "Data" );
00252 journal->setSummary( grp.readEntry( "name" ) );
00253 config.deleteGroup( "Data", KConfig::Localized );
00254 grp.changeGroup( "General" );
00255 grp.writeEntry( "version", KNOTES_VERSION );
00256 grp.changeGroup( "WindowDisplay" );
00257 #ifdef Q_WS_X11
00258 uint state = grp.readEntry( "state", uint( NET::SkipTaskbar ) );
00259 grp.writeEntry( "ShowInTaskbar",
00260 ( state & NET::SkipTaskbar ) ? false : true );
00261 grp.writeEntry( "KeepAbove",
00262 ( state & NET::KeepAbove ) ? true : false );
00263 #endif
00264 grp.deleteEntry( "state" );
00265
00266
00267 QFile infile( noteDir.absoluteFilePath( "." + file + "_data" ) );
00268 if ( infile.open( QIODevice::ReadOnly ) ) {
00269 QTextStream input( &infile );
00270 input.setCodec( "UTF-8" );
00271 journal->setDescription( input.readAll() );
00272 if ( !infile.remove() ) {
00273 kWarning( 5500 ) << "Could not delete data file: \"" << infile.fileName()
00274 << "\"";
00275 }
00276 }
00277 else
00278 kWarning( 5500 ) << "Could not open data file: \"" << infile.fileName()
00279 << "\"";
00280
00281 return true;
00282 }