Fix typo
[kdepim.git] / knotes / knoteslegacy.cpp
blobc67d8d090a43e74f2d6c1bfcb5673fbf3060c799
1 /*******************************************************************
2 KNotes -- Notes for the KDE project
4 Copyright (c) 2002-2004, Michael Brade <brade@kde.org>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************/
21 #include "knoteslegacy.h"
22 #include "knoteconfig.h"
23 #include "kdepim-version.h"
25 #include <QFile>
26 #include <QFont>
27 #include <QPoint>
28 #include <QColor>
29 #include <QStringList>
30 #include <QTextStream>
32 #include <kdebug.h>
33 #include <kapplication.h>
34 #include <kglobal.h>
35 #include <kurl.h>
36 #include <kstandarddirs.h>
37 #include <kconfig.h>
38 #include <kio/netaccess.h>
39 #include <kcal/calendarlocal.h>
40 #include <kcal/journal.h>
42 #include <unistd.h>
43 #include <netwm.h>
45 using namespace KCal;
48 void KNotesLegacy::cleanUp()
50 // remove old (KDE 1.x) local config file if it still exists
51 const QString configfile = KGlobal::dirs()->saveLocation( "config" ) + "knotesrc";
52 if ( QFile::exists( configfile ) ) {
53 KConfigGroup test(
54 KSharedConfig::openConfig( configfile, KConfig::SimpleConfig ),
55 "General" );
56 double version = test.readEntry( "version", 9999999.99 );
58 if ( version == 1.0 ) {
59 if ( !( KStandardDirs::checkAccess( configfile, W_OK ) &&
60 QFile::remove( configfile ) ) ) {
61 kError( 5500 ) << "Could not delete old config file" << configfile;
67 bool KNotesLegacy::convert( CalendarLocal *calendar )
69 bool converted = false;
71 QDir noteDir( KGlobal::dirs()->saveLocation( "appdata", "notes/" ) );
72 const QStringList notes = noteDir.entryList( QDir::Files, QDir::Name );
73 for ( QStringList::ConstIterator note = notes.constBegin(); note != notes.constEnd();
74 ++note ) {
75 QString file = noteDir.absoluteFilePath( *note );
76 KConfig *test = new KConfig( file, KConfig::SimpleConfig );
77 KConfigGroup grp( test, "General" );
78 double version = grp.readEntry( "version", 1.0 );
80 if ( version < 3.0 ) {
82 // create the new note
83 Journal *journal = new Journal();
84 bool success;
86 if ( version < 2.0 ) {
87 success = convertKNotes1Config( journal, noteDir, *note );
88 } else {
89 success = convertKNotes2Config( journal, noteDir, *note );
92 // could not convert file => do not add a new note
93 if ( !success ) {
94 delete journal;
95 } else {
96 calendar->addJournal( journal );
97 converted = true;
99 } else if ( version < 3.2 ) { // window state changed for version 3.2
100 #ifdef Q_WS_X11
101 uint state = grp.readEntry( "state", uint( NET::SkipTaskbar ) );
103 grp.writeEntry( "ShowInTaskbar",
104 ( state & NET::SkipTaskbar ) ? false : true );
105 grp.writeEntry( "KeepAbove",
106 ( state & NET::KeepAbove ) ? true : false );
107 #endif
108 grp.deleteEntry( "state" );
110 delete test;
113 return converted;
116 bool KNotesLegacy::convertKNotes1Config( Journal *journal, QDir &noteDir,
117 const QString &file )
119 QFile infile( noteDir.absoluteFilePath( file ) );
120 if ( !infile.open( QIODevice::ReadOnly ) ) {
121 kError( 5500 ) << "Could not open input file: \""
122 << infile.fileName() << "\"";
123 return false;
126 QTextStream input( &infile );
128 // get the name
129 journal->setSummary( input.readLine() );
131 const QStringList props = input.readLine().split( '+', QString::SkipEmptyParts );
133 // robustness
134 if ( props.count() != 13 ) {
135 kWarning( 5500 ) <<"The file \"" << infile.fileName()
136 << "\" lacks version information but is not a valid"
137 << "KNotes 1 config file either!";
138 return false;
141 // the new configfile's name
142 const QString configFile = noteDir.absoluteFilePath( journal->uid() );
144 // set the defaults
145 KIO::NetAccess::file_copy(
146 KUrl( KGlobal::dirs()->saveLocation( "config" ) + "knotesrc" ),
147 KUrl( configFile ),
151 KNoteConfig config( KSharedConfig::openConfig( configFile,
152 KConfig::NoGlobals ) );
153 config.readConfig();
154 config.setVersion( KDEPIM_VERSION );
156 // get the geometry
157 config.setWidth( props[3].toUInt() );
158 config.setHeight( props[4].toUInt() );
160 // get the background color
161 uint red = input.readLine().toUInt();
162 uint green = input.readLine().toUInt();
163 uint blue = input.readLine().toUInt();
164 config.setBgColor( QColor( red, green, blue ) );
166 // get the foreground color
167 red = input.readLine().toUInt();
168 green = input.readLine().toUInt();
169 blue = input.readLine().toUInt();
170 config.setFgColor( QColor( red, green, blue ) );
172 // get the font
173 QString fontfamily = input.readLine();
174 if ( fontfamily.isEmpty() )
175 fontfamily = QString( "Sans Serif" );
176 uint size = input.readLine().toUInt();
177 size = qMax( size, ( uint ) 4 );
178 uint weight = input.readLine().toUInt();
179 bool italic = ( input.readLine().toUInt() == 1 );
180 QFont font( fontfamily, size, weight, italic );
182 config.setTitleFont( font );
183 config.setFont( font );
185 // 3d frame? Not supported yet!
186 input.readLine();
188 // autoindent
189 config.setAutoIndent( input.readLine().toUInt() == 1 );
191 // KNotes 1 never had rich text
192 config.setRichText( false );
194 int note_desktop = props[0].toUInt();
196 // hidden or on all desktops?
197 if ( input.readLine().toUInt() == 1 )
198 note_desktop = 0;
199 #ifdef Q_WS_X11
200 else if ( props[11].toUInt() == 1 )
201 note_desktop = NETWinInfo::OnAllDesktops;
202 #endif
204 config.setDesktop( note_desktop );
205 config.setPosition( QPoint( props[1].toUInt(), props[2].toUInt() ) );
206 config.setKeepAbove( props[12].toUInt() & 2048 );
208 config.writeConfig();
210 // get the text
211 QString text;
212 while ( !input.atEnd() ) {
213 text.append( input.readLine() );
214 if ( !input.atEnd() ) {
215 text.append( '\n' );
219 journal->setDescription( text );
221 if ( !infile.remove() )
223 kWarning( 5500 ) << "Could not delete input file: \"" << infile.fileName()
224 << "\"";
227 return true;
230 bool KNotesLegacy::convertKNotes2Config( Journal *journal, QDir &noteDir,
231 const QString &file )
233 const QString configFile = noteDir.absoluteFilePath( journal->uid() );
235 // new name for config file
236 if ( !noteDir.rename( file, journal->uid() ) ) {
237 kError( 5500 ) << "Could not rename input file: \""
238 << noteDir.absoluteFilePath( file ) << "\" to \""
239 << configFile << "\"!";
240 return false;
243 // update the config
244 KConfig config( configFile );
245 KConfigGroup grp( &config, "Data" );
246 journal->setSummary( grp.readEntry( "name" ) );
247 config.deleteGroup( "Data", KConfig::Localized );
248 KConfigGroup _grp2(&config, "General" ); // XXX right?
249 _grp2.writeEntry( "version", KDEPIM_VERSION );
250 KConfigGroup _grp3(&config, "WindowDisplay" ); // XXX right?
251 #ifdef Q_WS_X11
252 uint state = _grp3.readEntry( "state", uint( NET::SkipTaskbar ) );
253 _grp3.writeEntry( "ShowInTaskbar",
254 ( state & NET::SkipTaskbar ) ? false : true );
255 _grp3.writeEntry( "KeepAbove",
256 ( state & NET::KeepAbove ) ? true : false );
257 #endif
258 _grp3.deleteEntry( "state" );
260 // load the saved text and put it in the journal
261 QFile infile( noteDir.absoluteFilePath( '.' + file + "_data" ) );
262 if ( infile.open( QIODevice::ReadOnly ) ) {
263 QTextStream input( &infile );
264 input.setCodec( "UTF-8" );
265 journal->setDescription( input.readAll() );
266 if ( !infile.remove() ) {
267 kWarning( 5500 ) << "Could not delete data file: \"" << infile.fileName()
268 << "\"";
271 else
272 kWarning( 5500 ) << "Could not open data file: \"" << infile.fileName()
273 << "\"";
275 return true;