knotes

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 <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     // remove old (KDE 1.x) local config file if it still exists
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     QStringList notes = noteDir.entryList( QDir::Files, QDir::Name );
00076     for ( QStringList::Iterator note = notes.begin(); note != notes.end(); 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             delete test;
00086 
00087             // create the new note
00088             Journal *journal = new Journal();
00089             bool success;
00090 
00091             if ( version < 2.0 )
00092                 success = convertKNotes1Config( journal, noteDir, *note );
00093             else
00094                 success = convertKNotes2Config( journal, noteDir, *note );
00095 
00096             // could not convert file => do not add a new note
00097             if ( !success )
00098                 delete journal;
00099             else
00100             {
00101                 calendar->addJournal( journal );
00102                 converted = true;
00103             }
00104         }
00105         // window state changed for version 3.2
00106         else if ( version < 3.2 )
00107         {
00108             uint state = test->readUnsignedLongNumEntry( "state", NET::SkipTaskbar );
00109             test->writeEntry( "ShowInTaskbar", (state & NET::SkipTaskbar) ? false : true );
00110             test->writeEntry( "KeepAbove", (state & NET::KeepAbove) ? true : false );
00111             test->deleteEntry( "state" );
00112             delete test;
00113         }
00114     }
00115 
00116     return converted;
00117 }
00118 
00119 bool KNotesLegacy::convertKNotes1Config( Journal *journal, QDir& noteDir,
00120         const QString& file )
00121 {
00122     QFile infile( noteDir.absFilePath( file ) );
00123     if ( !infile.open( IO_ReadOnly ) )
00124     {
00125         kdError(5500) << k_funcinfo << "Could not open input file: \""
00126                       << infile.name() << "\"" << endl;
00127         return false;
00128     }
00129 
00130     QTextStream input( &infile );
00131 
00132     // get the name
00133     journal->setSummary( input.readLine() );
00134 
00135     QStringList props = QStringList::split( '+', input.readLine() );
00136 
00137     // robustness
00138     if ( props.count() != 13 )
00139     {
00140         kdWarning(5500) << k_funcinfo << "The file \"" << infile.name()
00141                         << "\" lacks version information but is not a valid "
00142                         << "KNotes 1 config file either!" << endl;
00143         return false;
00144     }
00145 
00146     // the new configfile's name
00147     QString configFile = noteDir.absFilePath( journal->uid() );
00148 
00149     // set the defaults
00150     KIO::NetAccess::copy(
00151         KURL( KGlobal::dirs()->saveLocation( "config" ) + "knotesrc" ),
00152         KURL( configFile ),
00153         0
00154     );
00155 
00156     KNoteConfig config( KSharedConfig::openConfig( configFile, false, false ) );
00157     config.readConfig();
00158     config.setVersion( KNOTES_VERSION );
00159 
00160     // get the geometry
00161     config.setWidth( props[3].toUInt() );
00162     config.setHeight( props[4].toUInt() );
00163 
00164     // get the background color
00165     uint red = input.readLine().toUInt();
00166     uint green = input.readLine().toUInt();
00167     uint blue = input.readLine().toUInt();
00168     config.setBgColor( QColor( red, green, blue ) );
00169 
00170     // get the foreground color
00171     red = input.readLine().toUInt();
00172     green = input.readLine().toUInt();
00173     blue = input.readLine().toUInt();
00174     config.setFgColor( QColor( red, green, blue ) );
00175 
00176     // get the font
00177     QString fontfamily = input.readLine();
00178     if ( fontfamily.isEmpty() )
00179         fontfamily = QString( "Sans Serif" );
00180     uint size = input.readLine().toUInt();
00181     size = QMAX( size, 4 );
00182     uint weight = input.readLine().toUInt();
00183     bool italic = ( input.readLine().toUInt() == 1 );
00184     QFont font( fontfamily, size, weight, italic );
00185 
00186     config.setTitleFont( font );
00187     config.setFont( font );
00188 
00189     // 3d frame? Not supported yet!
00190     input.readLine();
00191 
00192     // autoindent
00193     config.setAutoIndent( input.readLine().toUInt() == 1 );
00194 
00195     // KNotes 1 never had rich text
00196     config.setRichText( false );
00197 
00198     int note_desktop = props[0].toUInt();
00199 
00200     // hidden or on all desktops?
00201     if ( input.readLine().toUInt() == 1 )
00202         note_desktop = 0;
00203     else if ( props[11].toUInt() == 1 )
00204         note_desktop = NETWinInfo::OnAllDesktops;
00205 
00206     config.setDesktop( note_desktop );
00207     config.setPosition( QPoint( props[1].toUInt(), props[2].toUInt() ) );
00208     config.setKeepAbove( props[12].toUInt() & 2048 );
00209 
00210     config.writeConfig();
00211 
00212     // get the text
00213     QString text;
00214     while ( !input.atEnd() )
00215     {
00216         text.append( input.readLine() );
00217         if ( !input.atEnd() )
00218             text.append( '\n' );
00219     }
00220 
00221     journal->setDescription( text );
00222 
00223     if ( !infile.remove() )
00224         kdWarning(5500) << k_funcinfo << "Could not delete input file: \"" << infile.name() << "\"" << endl;
00225 
00226     return true;
00227 }
00228 
00229 bool KNotesLegacy::convertKNotes2Config( Journal *journal, QDir& noteDir,
00230         const QString& file )
00231 {
00232     QString configFile = noteDir.absFilePath( journal->uid() );
00233 
00234     // new name for config file
00235     if ( !noteDir.rename( file, journal->uid() ) )
00236     {
00237         kdError(5500) << k_funcinfo << "Could not rename input file: \""
00238                       << noteDir.absFilePath( file ) << "\" to \""
00239                       << configFile << "\"!" << endl;
00240         return false;
00241     }
00242 
00243     // update the config
00244     KConfig config( configFile );
00245     config.setGroup( "Data" );
00246     journal->setSummary( config.readEntry( "name" ) );
00247     config.deleteGroup( "Data", true );
00248     config.setGroup( "General" );
00249     config.writeEntry( "version", KNOTES_VERSION );
00250     config.setGroup( "WindowDisplay" );
00251     uint state = config.readUnsignedLongNumEntry( "state", NET::SkipTaskbar );
00252     config.writeEntry( "ShowInTaskbar", (state & NET::SkipTaskbar) ? false : true );
00253     config.writeEntry( "KeepAbove", (state & NET::KeepAbove) ? true : false );
00254     config.deleteEntry( "state" );
00255 
00256     // load the saved text and put it in the journal
00257     QFile infile( noteDir.absFilePath( "." + file + "_data" ) );
00258     if ( infile.open( IO_ReadOnly ) )
00259     {
00260         QTextStream input( &infile );
00261         input.setEncoding( QTextStream::UnicodeUTF8 );
00262         journal->setDescription( input.read() );
00263         if ( !infile.remove() )
00264             kdWarning(5500) << k_funcinfo << "Could not delete data file: \"" << infile.name() << "\"" << endl;
00265     }
00266     else
00267         kdWarning(5500) << k_funcinfo << "Could not open data file: \"" << infile.name() << "\"" << endl;
00268 
00269     return true;
00270 }
KDE Home | KDE Accessibility Home | Description of Access Keys