knotes Library API Documentation

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