• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

knotes

  • sources
  • kde-4.12
  • kdepim
  • knotes
  • migrations
knoteslegacy.cpp
Go to the documentation of this file.
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 "knoteslegacy.h"
22 #include "knoteconfig.h"
23 #include "kdepim-version.h"
24 
25 #include <QFile>
26 #include <QFont>
27 #include <QPoint>
28 #include <QColor>
29 #include <QStringList>
30 #include <QTextStream>
31 
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>
41 
42 #include <unistd.h>
43 #include <netwm.h>
44 
45 using namespace KCal;
46 
47 
48 void KNotesLegacy::cleanUp()
49 {
50  // remove old (KDE 1.x) local config file if it still exists
51  const QString configfile = KGlobal::dirs()->saveLocation( "config" ) + QLatin1String("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 );
57 
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;
62  }
63  }
64  }
65 }
66 
67 bool KNotesLegacy::convert( CalendarLocal *calendar )
68 {
69  bool converted = false;
70 
71  QDir noteDir( KGlobal::dirs()->saveLocation( "appdata", QLatin1String("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 );
79 
80  if ( version < 3.0 ) {
81 
82  // create the new note
83  Journal *journal = new Journal();
84  bool success;
85 
86  if ( version < 2.0 ) {
87  success = convertKNotes1Config( journal, noteDir, *note );
88  } else {
89  success = convertKNotes2Config( journal, noteDir, *note );
90  }
91 
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;
98  }
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 ) );
102 
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" );
109  }
110  delete test;
111  }
112 
113  return converted;
114 }
115 
116 bool KNotesLegacy::convertKNotes1Config( Journal *journal, QDir &noteDir,
117  const QString &file )
118 {
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;
124  }
125 
126  QTextStream input( &infile );
127 
128  // get the name
129  journal->setSummary( input.readLine() );
130 
131  const QStringList props = input.readLine().split( QLatin1Char('+'), QString::SkipEmptyParts );
132 
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;
139  }
140 
141  // the new configfile's name
142  const QString configFile = noteDir.absoluteFilePath( journal->uid() );
143 
144  // set the defaults
145  KIO::NetAccess::file_copy(
146  KUrl( KGlobal::dirs()->saveLocation( "config" ) + QLatin1String("knotesrc") ),
147  KUrl( configFile ),
148  0
149  );
150 
151  KNoteConfig config( KSharedConfig::openConfig( configFile,
152  KConfig::NoGlobals ) );
153  config.readConfig();
154  config.setVersion( QLatin1String(KDEPIM_VERSION) );
155 
156  // get the geometry
157  config.setWidth( props[3].toUInt() );
158  config.setHeight( props[4].toUInt() );
159 
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 ) );
165 
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 ) );
171 
172  // get the font
173  QString fontfamily = input.readLine();
174  if ( fontfamily.isEmpty() )
175  fontfamily = QLatin1String( "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 );
181 
182  config.setTitleFont( font );
183  config.setFont( font );
184 
185  // 3d frame? Not supported yet!
186  input.readLine();
187 
188  // autoindent
189  config.setAutoIndent( input.readLine().toUInt() == 1 );
190 
191  // KNotes 1 never had rich text
192  config.setRichText( false );
193 
194  int note_desktop = props[0].toUInt();
195 
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
203 
204  config.setDesktop( note_desktop );
205  config.setPosition( QPoint( props[1].toUInt(), props[2].toUInt() ) );
206  config.setKeepAbove( props[12].toUInt() & 2048 );
207 
208  config.writeConfig();
209 
210  // get the text
211  QString text;
212  while ( !input.atEnd() ) {
213  text.append( input.readLine() );
214  if ( !input.atEnd() ) {
215  text.append( QLatin1Char('\n') );
216  }
217  }
218 
219  journal->setDescription( text );
220 
221  if ( !infile.remove() )
222  {
223  kWarning( 5500 ) << "Could not delete input file: \"" << infile.fileName()
224  << "\"";
225  }
226 
227  return true;
228 }
229 
230 bool KNotesLegacy::convertKNotes2Config( Journal *journal, QDir &noteDir,
231  const QString &file )
232 {
233  const QString configFile = noteDir.absoluteFilePath( journal->uid() );
234 
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;
241  }
242 
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" );
259 
260  // load the saved text and put it in the journal
261  QFile infile( noteDir.absoluteFilePath( QLatin1Char('.') + file + QLatin1String("_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  << "\"";
269  }
270  }
271  else
272  kWarning( 5500 ) << "Could not open data file: \"" << infile.fileName()
273  << "\"";
274 
275  return true;
276 }
KNoteConfig
Definition: knoteconfig.h:13
KNotesLegacy::cleanUp
static void cleanUp()
Definition: knoteslegacy.cpp:48
KNotesLegacy::convert
static bool convert(KCal::CalendarLocal *calendar)
Definition: knoteslegacy.cpp:67
knoteslegacy.h
knoteconfig.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:33 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

knotes

Skip menu "knotes"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal