knotes

knoteslegacy.cpp
1 /*******************************************************************
2  KNotes -- Notes for the KDE project
3 
4  Copyright (c) 2002-2004, Michael Brade <brade@kde.org>
5 
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.
10 
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.
15 
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 *******************************************************************/
20 
21 #include <tqfile.h>
22 #include <tqfont.h>
23 #include <tqpoint.h>
24 #include <tqcolor.h>
25 #include <tqstringlist.h>
26 #include <tqtextstream.h>
27 
28 #include <kdebug.h>
29 #include <tdeapplication.h>
30 #include <tdeglobal.h>
31 #include <kurl.h>
32 #include <kstandarddirs.h>
33 #include <ksimpleconfig.h>
34 #include <tdeio/netaccess.h>
35 
36 #include <unistd.h>
37 
38 #include "knoteslegacy.h"
39 #include "knoteconfig.h"
40 #include "version.h"
41 
42 #include "libkcal/calendarlocal.h"
43 #include "libkcal/journal.h"
44 
45 #include <netwm.h>
46 
47 using namespace KCal;
48 
49 
50 void KNotesLegacy::cleanUp()
51 {
52  // remove old (KDE 1.x) local config file if it still exists
53  TQString configfile = TDEGlobal::dirs()->saveLocation( "config" ) + "knotesrc";
54  if ( TQFile::exists( configfile ) ) {
55  KSimpleConfig *test = new KSimpleConfig( configfile );
56  test->setGroup( "General" );
57  double version = test->readDoubleNumEntry( "version", 1.0 );
58  delete test;
59 
60  if ( version == 1.0 ) {
61  if ( !( checkAccess( configfile, W_OK ) &&
62  TQFile::remove( configfile ) ) )
63  {
64  kdError(5500) << k_funcinfo << "Could not delete old config file " << configfile << endl;
65  }
66  }
67  }
68 }
69 
70 bool KNotesLegacy::convert( CalendarLocal *calendar )
71 {
72  bool converted = false;
73 
74  TQDir noteDir( TDEGlobal::dirs()->saveLocation( "appdata", "notes/" ) );
75  const TQStringList notes = noteDir.entryList( TQDir::Files, TQDir::Name );
76  for ( TQStringList::ConstIterator note = notes.constBegin(); note != notes.constEnd(); ++note )
77  {
78  TQString file = noteDir.absFilePath( *note );
79  KSimpleConfig* test = new KSimpleConfig( file );
80  test->setGroup( "General" );
81  double version = test->readDoubleNumEntry( "version", 1.0 );
82 
83  if ( version < 3.0 )
84  {
85 
86  // create the new note
87  Journal *journal = new Journal();
88  bool success;
89 
90  if ( version < 2.0 )
91  success = convertKNotes1Config( journal, noteDir, *note );
92  else
93  success = convertKNotes2Config( journal, noteDir, *note );
94 
95  // could not convert file => do not add a new note
96  if ( !success )
97  delete journal;
98  else
99  {
100  calendar->addJournal( journal );
101  converted = true;
102  }
103  }
104  // window state changed for version 3.2
105  else if ( version < 3.2 )
106  {
107  uint state = test->readUnsignedLongNumEntry( "state", NET::SkipTaskbar );
108  test->writeEntry( "ShowInTaskbar", (state & NET::SkipTaskbar) ? false : true );
109  test->writeEntry( "KeepAbove", (state & NET::KeepAbove) ? true : false );
110  test->deleteEntry( "state" );
111  }
112  delete test;
113  }
114 
115  return converted;
116 }
117 
118 bool KNotesLegacy::convertKNotes1Config( Journal *journal, TQDir& noteDir,
119  const TQString& file )
120 {
121  TQFile infile( noteDir.absFilePath( file ) );
122  if ( !infile.open( IO_ReadOnly ) )
123  {
124  kdError(5500) << k_funcinfo << "Could not open input file: \""
125  << infile.name() << "\"" << endl;
126  return false;
127  }
128 
129  TQTextStream input( &infile );
130 
131  // get the name
132  journal->setSummary( input.readLine() );
133 
134  TQStringList props = TQStringList::split( '+', input.readLine() );
135 
136  // robustness
137  if ( props.count() != 13 )
138  {
139  kdWarning(5500) << k_funcinfo << "The file \"" << infile.name()
140  << "\" lacks version information but is not a valid "
141  << "KNotes 1 config file either!" << endl;
142  return false;
143  }
144 
145  // the new configfile's name
146  TQString configFile = noteDir.absFilePath( journal->uid() );
147 
148  // set the defaults
149  TDEIO::NetAccess::copy(
150  KURL( TDEGlobal::dirs()->saveLocation( "config" ) + "knotesrc" ),
151  KURL( configFile ),
152  0
153  );
154 
155  KNoteConfig config( TDESharedConfig::openConfig( configFile, false, false ) );
156  config.readConfig();
157  config.setVersion( KNOTES_VERSION );
158 
159  // get the geometry
160  config.setWidth( props[3].toUInt() );
161  config.setHeight( props[4].toUInt() );
162 
163  // get the background color
164  uint red = input.readLine().toUInt();
165  uint green = input.readLine().toUInt();
166  uint blue = input.readLine().toUInt();
167  config.setBgColor( TQColor( red, green, blue ) );
168 
169  // get the foreground color
170  red = input.readLine().toUInt();
171  green = input.readLine().toUInt();
172  blue = input.readLine().toUInt();
173  config.setFgColor( TQColor( red, green, blue ) );
174 
175  // get the font
176  TQString fontfamily = input.readLine();
177  if ( fontfamily.isEmpty() )
178  fontfamily = TQString( "Sans Serif" );
179  uint size = input.readLine().toUInt();
180  size = TQMAX( size, 4 );
181  uint weight = input.readLine().toUInt();
182  bool italic = ( input.readLine().toUInt() == 1 );
183  TQFont font( fontfamily, size, weight, italic );
184 
185  config.setTitleFont( font );
186  config.setFont( font );
187 
188  // 3d frame? Not supported yet!
189  input.readLine();
190 
191  // autoindent
192  config.setAutoIndent( input.readLine().toUInt() == 1 );
193 
194  // KNotes 1 never had rich text
195  config.setRichText( false );
196 
197  int note_desktop = props[0].toUInt();
198 
199  // hidden or on all desktops?
200  if ( input.readLine().toUInt() == 1 )
201  note_desktop = 0;
202  else if ( props[11].toUInt() == 1 )
203  note_desktop = NETWinInfo::OnAllDesktops;
204 
205  config.setDesktop( note_desktop );
206  config.setPosition( TQPoint( props[1].toUInt(), props[2].toUInt() ) );
207  config.setKeepAbove( props[12].toUInt() & 2048 );
208 
209  config.writeConfig();
210 
211  // get the text
212  TQString text;
213  while ( !input.atEnd() )
214  {
215  text.append( input.readLine() );
216  if ( !input.atEnd() )
217  text.append( '\n' );
218  }
219 
220  journal->setDescription( text );
221 
222  if ( !infile.remove() )
223  kdWarning(5500) << k_funcinfo << "Could not delete input file: \"" << infile.name() << "\"" << endl;
224 
225  return true;
226 }
227 
228 bool KNotesLegacy::convertKNotes2Config( Journal *journal, TQDir& noteDir,
229  const TQString& file )
230 {
231  TQString configFile = noteDir.absFilePath( journal->uid() );
232 
233  // new name for config file
234  if ( !noteDir.rename( file, journal->uid() ) )
235  {
236  kdError(5500) << k_funcinfo << "Could not rename input file: \""
237  << noteDir.absFilePath( file ) << "\" to \""
238  << configFile << "\"!" << endl;
239  return false;
240  }
241 
242  // update the config
243  TDEConfig config( configFile );
244  config.setGroup( "Data" );
245  journal->setSummary( config.readEntry( "name" ) );
246  config.deleteGroup( "Data", true );
247  config.setGroup( "General" );
248  config.writeEntry( "version", KNOTES_VERSION );
249  config.setGroup( "WindowDisplay" );
250  uint state = config.readUnsignedLongNumEntry( "state", NET::SkipTaskbar );
251  config.writeEntry( "ShowInTaskbar", (state & NET::SkipTaskbar) ? false : true );
252  config.writeEntry( "KeepAbove", (state & NET::KeepAbove) ? true : false );
253  config.deleteEntry( "state" );
254 
255  // load the saved text and put it in the journal
256  TQFile infile( noteDir.absFilePath( "." + file + "_data" ) );
257  if ( infile.open( IO_ReadOnly ) )
258  {
259  TQTextStream input( &infile );
260  input.setEncoding( TQTextStream::UnicodeUTF8 );
261  journal->setDescription( input.read() );
262  if ( !infile.remove() )
263  kdWarning(5500) << k_funcinfo << "Could not delete data file: \"" << infile.name() << "\"" << endl;
264  }
265  else
266  kdWarning(5500) << k_funcinfo << "Could not open data file: \"" << infile.name() << "\"" << endl;
267 
268  return true;
269 }