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"
29 #include <QStringList>
30 #include <QTextStream>
33 #include <kapplication.h>
36 #include <kstandarddirs.h>
38 #include <kio/netaccess.h>
39 #include <kcal/calendarlocal.h>
40 #include <kcal/journal.h>
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
) ) {
54 KSharedConfig::openConfig( configfile
, KConfig::SimpleConfig
),
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();
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();
86 if ( version
< 2.0 ) {
87 success
= convertKNotes1Config( journal
, noteDir
, *note
);
89 success
= convertKNotes2Config( journal
, noteDir
, *note
);
92 // could not convert file => do not add a new note
96 calendar
->addJournal( journal
);
99 } else if ( version
< 3.2 ) { // window state changed for version 3.2
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 );
108 grp
.deleteEntry( "state" );
116 bool KNotesLegacy::convertKNotes1Config( Journal
*journal
, QDir
¬eDir
,
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() << "\"";
126 QTextStream
input( &infile
);
129 journal
->setSummary( input
.readLine() );
131 const QStringList props
= input
.readLine().split( '+', QString::SkipEmptyParts
);
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!";
141 // the new configfile's name
142 const QString configFile
= noteDir
.absoluteFilePath( journal
->uid() );
145 KIO::NetAccess::file_copy(
146 KUrl( KGlobal::dirs()->saveLocation( "config" ) + "knotesrc" ),
151 KNoteConfig
config( KSharedConfig::openConfig( configFile
,
152 KConfig::NoGlobals
) );
154 config
.setVersion( KDEPIM_VERSION
);
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
) );
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!
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 )
200 else if ( props
[11].toUInt() == 1 )
201 note_desktop
= NETWinInfo::OnAllDesktops
;
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();
212 while ( !input
.atEnd() ) {
213 text
.append( input
.readLine() );
214 if ( !input
.atEnd() ) {
219 journal
->setDescription( text
);
221 if ( !infile
.remove() )
223 kWarning( 5500 ) << "Could not delete input file: \"" << infile
.fileName()
230 bool KNotesLegacy::convertKNotes2Config( Journal
*journal
, QDir
¬eDir
,
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
<< "\"!";
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?
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 );
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()
272 kWarning( 5500 ) << "Could not open data file: \"" << infile
.fileName()