TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

270 lines
8.5 KiB

  1. /*******************************************************************
  2. KNotes -- Notes for the KDE project
  3. Copyright (c) 2002-2004, Michael Brade <brade@kde.org>
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *******************************************************************/
  16. #include <tqfile.h>
  17. #include <tqfont.h>
  18. #include <tqpoint.h>
  19. #include <tqcolor.h>
  20. #include <tqstringlist.h>
  21. #include <tqtextstream.h>
  22. #include <kdebug.h>
  23. #include <tdeapplication.h>
  24. #include <tdeglobal.h>
  25. #include <kurl.h>
  26. #include <kstandarddirs.h>
  27. #include <ksimpleconfig.h>
  28. #include <tdeio/netaccess.h>
  29. #include <unistd.h>
  30. #include "knoteslegacy.h"
  31. #include "knoteconfig.h"
  32. #include "version.h"
  33. #include "libkcal/calendarlocal.h"
  34. #include "libkcal/journal.h"
  35. #include <netwm.h>
  36. using namespace KCal;
  37. void KNotesLegacy::cleanUp()
  38. {
  39. // remove old (KDE 1.x) local config file if it still exists
  40. TQString configfile = TDEGlobal::dirs()->saveLocation( "config" ) + "knotesrc";
  41. if ( TQFile::exists( configfile ) ) {
  42. KSimpleConfig *test = new KSimpleConfig( configfile );
  43. test->setGroup( "General" );
  44. double version = test->readDoubleNumEntry( "version", 1.0 );
  45. delete test;
  46. if ( version == 1.0 ) {
  47. if ( !( checkAccess( configfile, W_OK ) &&
  48. TQFile::remove( configfile ) ) )
  49. {
  50. kdError(5500) << k_funcinfo << "Could not delete old config file " << configfile << endl;
  51. }
  52. }
  53. }
  54. }
  55. bool KNotesLegacy::convert( CalendarLocal *calendar )
  56. {
  57. bool converted = false;
  58. TQDir noteDir( TDEGlobal::dirs()->saveLocation( "appdata", "notes/" ) );
  59. const TQStringList notes = noteDir.entryList( TQDir::Files, TQDir::Name );
  60. for ( TQStringList::ConstIterator note = notes.constBegin(); note != notes.constEnd(); ++note )
  61. {
  62. TQString file = noteDir.absFilePath( *note );
  63. KSimpleConfig* test = new KSimpleConfig( file );
  64. test->setGroup( "General" );
  65. double version = test->readDoubleNumEntry( "version", 1.0 );
  66. if ( version < 3.0 )
  67. {
  68. // create the new note
  69. Journal *journal = new Journal();
  70. bool success;
  71. if ( version < 2.0 )
  72. success = convertKNotes1Config( journal, noteDir, *note );
  73. else
  74. success = convertKNotes2Config( journal, noteDir, *note );
  75. // could not convert file => do not add a new note
  76. if ( !success )
  77. delete journal;
  78. else
  79. {
  80. calendar->addJournal( journal );
  81. converted = true;
  82. }
  83. }
  84. // window state changed for version 3.2
  85. else if ( version < 3.2 )
  86. {
  87. uint state = test->readUnsignedLongNumEntry( "state", NET::SkipTaskbar );
  88. test->writeEntry( "ShowInTaskbar", (state & NET::SkipTaskbar) ? false : true );
  89. test->writeEntry( "KeepAbove", (state & NET::KeepAbove) ? true : false );
  90. test->deleteEntry( "state" );
  91. }
  92. delete test;
  93. }
  94. return converted;
  95. }
  96. bool KNotesLegacy::convertKNotes1Config( Journal *journal, TQDir& noteDir,
  97. const TQString& file )
  98. {
  99. TQFile infile( noteDir.absFilePath( file ) );
  100. if ( !infile.open( IO_ReadOnly ) )
  101. {
  102. kdError(5500) << k_funcinfo << "Could not open input file: \""
  103. << infile.name() << "\"" << endl;
  104. return false;
  105. }
  106. TQTextStream input( &infile );
  107. // get the name
  108. journal->setSummary( input.readLine() );
  109. TQStringList props = TQStringList::split( '+', input.readLine() );
  110. // robustness
  111. if ( props.count() != 13 )
  112. {
  113. kdWarning(5500) << k_funcinfo << "The file \"" << infile.name()
  114. << "\" lacks version information but is not a valid "
  115. << "KNotes 1 config file either!" << endl;
  116. return false;
  117. }
  118. // the new configfile's name
  119. TQString configFile = noteDir.absFilePath( journal->uid() );
  120. // set the defaults
  121. TDEIO::NetAccess::copy(
  122. KURL( TDEGlobal::dirs()->saveLocation( "config" ) + "knotesrc" ),
  123. KURL( configFile ),
  124. 0
  125. );
  126. KNoteConfig config( TDESharedConfig::openConfig( configFile, false, false ) );
  127. config.readConfig();
  128. config.setVersion( KNOTES_VERSION );
  129. // get the geometry
  130. config.setWidth( props[3].toUInt() );
  131. config.setHeight( props[4].toUInt() );
  132. // get the background color
  133. uint red = input.readLine().toUInt();
  134. uint green = input.readLine().toUInt();
  135. uint blue = input.readLine().toUInt();
  136. config.setBgColor( TQColor( red, green, blue ) );
  137. // get the foreground color
  138. red = input.readLine().toUInt();
  139. green = input.readLine().toUInt();
  140. blue = input.readLine().toUInt();
  141. config.setFgColor( TQColor( red, green, blue ) );
  142. // get the font
  143. TQString fontfamily = input.readLine();
  144. if ( fontfamily.isEmpty() )
  145. fontfamily = TQString( "Sans Serif" );
  146. uint size = input.readLine().toUInt();
  147. size = TQMAX( size, 4 );
  148. uint weight = input.readLine().toUInt();
  149. bool italic = ( input.readLine().toUInt() == 1 );
  150. TQFont font( fontfamily, size, weight, italic );
  151. config.setTitleFont( font );
  152. config.setFont( font );
  153. // 3d frame? Not supported yet!
  154. input.readLine();
  155. // autoindent
  156. config.setAutoIndent( input.readLine().toUInt() == 1 );
  157. // KNotes 1 never had rich text
  158. config.setRichText( false );
  159. int note_desktop = props[0].toUInt();
  160. // hidden or on all desktops?
  161. if ( input.readLine().toUInt() == 1 )
  162. note_desktop = 0;
  163. else if ( props[11].toUInt() == 1 )
  164. note_desktop = NETWinInfo::OnAllDesktops;
  165. config.setDesktop( note_desktop );
  166. config.setPosition( TQPoint( props[1].toUInt(), props[2].toUInt() ) );
  167. config.setKeepAbove( props[12].toUInt() & 2048 );
  168. config.writeConfig();
  169. // get the text
  170. TQString text;
  171. while ( !input.atEnd() )
  172. {
  173. text.append( input.readLine() );
  174. if ( !input.atEnd() )
  175. text.append( '\n' );
  176. }
  177. journal->setDescription( text );
  178. if ( !infile.remove() )
  179. kdWarning(5500) << k_funcinfo << "Could not delete input file: \"" << infile.name() << "\"" << endl;
  180. return true;
  181. }
  182. bool KNotesLegacy::convertKNotes2Config( Journal *journal, TQDir& noteDir,
  183. const TQString& file )
  184. {
  185. TQString configFile = noteDir.absFilePath( journal->uid() );
  186. // new name for config file
  187. if ( !noteDir.rename( file, journal->uid() ) )
  188. {
  189. kdError(5500) << k_funcinfo << "Could not rename input file: \""
  190. << noteDir.absFilePath( file ) << "\" to \""
  191. << configFile << "\"!" << endl;
  192. return false;
  193. }
  194. // update the config
  195. TDEConfig config( configFile );
  196. config.setGroup( "Data" );
  197. journal->setSummary( config.readEntry( "name" ) );
  198. config.deleteGroup( "Data", true );
  199. config.setGroup( "General" );
  200. config.writeEntry( "version", KNOTES_VERSION );
  201. config.setGroup( "WindowDisplay" );
  202. uint state = config.readUnsignedLongNumEntry( "state", NET::SkipTaskbar );
  203. config.writeEntry( "ShowInTaskbar", (state & NET::SkipTaskbar) ? false : true );
  204. config.writeEntry( "KeepAbove", (state & NET::KeepAbove) ? true : false );
  205. config.deleteEntry( "state" );
  206. // load the saved text and put it in the journal
  207. TQFile infile( noteDir.absFilePath( "." + file + "_data" ) );
  208. if ( infile.open( IO_ReadOnly ) )
  209. {
  210. TQTextStream input( &infile );
  211. input.setEncoding( TQTextStream::UnicodeUTF8 );
  212. journal->setDescription( input.read() );
  213. if ( !infile.remove() )
  214. kdWarning(5500) << k_funcinfo << "Could not delete data file: \"" << infile.name() << "\"" << endl;
  215. }
  216. else
  217. kdWarning(5500) << k_funcinfo << "Could not open data file: \"" << infile.name() << "\"" << endl;
  218. return true;
  219. }