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.
 
 
 
 
 
 

337 lines
9.3 KiB

  1. /*
  2. This file is part of tdepim.
  3. Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library 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 GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include <tqapplication.h>
  18. #include <tqstringlist.h>
  19. #include <kdebug.h>
  20. #include <tdelocale.h>
  21. #include <kstandarddirs.h>
  22. #include <kstringhandler.h>
  23. #include <kurl.h>
  24. #include "libkcal/journal.h"
  25. #include "knotes/resourcemanager.h"
  26. #include "knotes_egroupwareprefs.h"
  27. #include "knotes_resourcexmlrpc.h"
  28. #include "knotes_resourcexmlrpcconfig.h"
  29. #include "synchronizer.h"
  30. #include "xmlrpciface.h"
  31. using namespace KNotes;
  32. typedef KRES::PluginFactory< ResourceXMLRPC, ResourceXMLRPCConfig> XMLRPCFactory;
  33. K_EXPORT_COMPONENT_FACTORY( knotes_xmlrpc, XMLRPCFactory )
  34. static const TQString SearchNotesCommand = "infolog.boinfolog.search";
  35. static const TQString AddNoteCommand = "infolog.boinfolog.write";
  36. static const TQString DeleteNoteCommand = "infolog.boinfolog.delete";
  37. static const TQString LoadNoteCategoriesCommand = "infolog.boinfolog.categories";
  38. ResourceXMLRPC::ResourceXMLRPC( const TDEConfig* config )
  39. : ResourceNotes( config ), mCalendar( TQString::fromLatin1("UTC") ),
  40. mServer( 0 )
  41. {
  42. init();
  43. mPrefs->addGroupPrefix( identifier() );
  44. if ( config ) {
  45. readConfig( config );
  46. } else {
  47. setResourceName( i18n( "eGroupware Server" ) );
  48. }
  49. }
  50. ResourceXMLRPC::ResourceXMLRPC( )
  51. : ResourceNotes( 0 ), mCalendar( TQString::fromLatin1("UTC") ),
  52. mServer( 0 )
  53. {
  54. init();
  55. mPrefs->addGroupPrefix( identifier() );
  56. }
  57. ResourceXMLRPC::~ResourceXMLRPC()
  58. {
  59. delete mServer;
  60. mServer = 0;
  61. delete mPrefs;
  62. mPrefs = 0;
  63. delete mSynchronizer;
  64. mSynchronizer = 0;
  65. }
  66. void ResourceXMLRPC::init()
  67. {
  68. setType( "xmlrpc" );
  69. mPrefs = new EGroupwarePrefs;
  70. mSynchronizer = new Synchronizer;
  71. }
  72. void ResourceXMLRPC::readConfig( const TDEConfig* )
  73. {
  74. mPrefs->readConfig();
  75. }
  76. void ResourceXMLRPC::writeConfig( TDEConfig* config )
  77. {
  78. ResourceNotes::writeConfig( config );
  79. mPrefs->writeConfig();
  80. }
  81. bool ResourceXMLRPC::load()
  82. {
  83. mCalendar.close();
  84. if ( mServer )
  85. delete mServer;
  86. mServer = new KXMLRPC::Server( KURL(), this );
  87. mServer->setUrl( KURL( mPrefs->url() ) );
  88. mServer->setUserAgent( "KDE-Notes" );
  89. TQStringVariantMap args, columns;
  90. args.insert( "domain", mPrefs->domain() );
  91. args.insert( "username", mPrefs->user() );
  92. args.insert( "password", mPrefs->password() );
  93. mServer->call( "system.login", TQVariant( args ),
  94. this, TQT_SLOT( loginFinished( const TQValueList<TQVariant>&, const TQVariant& ) ),
  95. this, TQT_SLOT( fault( int, const TQString&, const TQVariant& ) ) );
  96. mSynchronizer->start();
  97. columns.insert( "type", "note" );
  98. args.clear();
  99. args.insert( "filter", "none" );
  100. args.insert( "col_filter", columns );
  101. args.insert( "order", "id_parent" );
  102. mServer->call( SearchNotesCommand, args,
  103. this, TQT_SLOT( listNotesFinished( const TQValueList<TQVariant>&, const TQVariant& ) ),
  104. this, TQT_SLOT( fault( int, const TQString&, const TQVariant& ) ) );
  105. mSynchronizer->start();
  106. return true;
  107. }
  108. bool ResourceXMLRPC::save()
  109. {
  110. mCalendar.close();
  111. return true;
  112. }
  113. bool ResourceXMLRPC::addNote( KCal::Journal *journal )
  114. {
  115. TQStringVariantMap args;
  116. writeNote( journal, args );
  117. KCal::Journal *oldJournal = mCalendar.journal( journal->uid() );
  118. bool added = false;
  119. if ( oldJournal ) {
  120. if ( !oldJournal->isReadOnly() ) {
  121. writeNote( journal, args );
  122. args.insert( "id", mUidMap[ journal->uid() ].toInt() );
  123. mServer->call( AddNoteCommand, TQVariant( args ),
  124. this, TQT_SLOT( updateNoteFinished( const TQValueList<TQVariant>&, const TQVariant& ) ),
  125. this, TQT_SLOT( fault( int, const TQString&, const TQVariant& ) ) );
  126. mCalendar.addJournal( journal );
  127. added = true;
  128. }
  129. } else {
  130. mServer->call( AddNoteCommand, TQVariant( args ),
  131. this, TQT_SLOT( addNoteFinished( const TQValueList<TQVariant>&, const TQVariant& ) ),
  132. this, TQT_SLOT( fault( int, const TQString&, const TQVariant& ) ),
  133. TQVariant( journal->uid() ) );
  134. mCalendar.addJournal( journal );
  135. added = true;
  136. }
  137. if ( added )
  138. mSynchronizer->start();
  139. return true;
  140. }
  141. bool ResourceXMLRPC::deleteNote( KCal::Journal *journal )
  142. {
  143. int id = mUidMap[ journal->uid() ].toInt();
  144. mServer->call( DeleteNoteCommand, id,
  145. this, TQT_SLOT( deleteNoteFinished( const TQValueList<TQVariant>&, const TQVariant& ) ),
  146. this, TQT_SLOT( fault( int, const TQString&, const TQVariant& ) ),
  147. TQVariant( journal->uid() ) );
  148. mSynchronizer->start();
  149. return true;
  150. }
  151. KCal::Alarm::List ResourceXMLRPC::alarms( const TQDateTime& from, const TQDateTime& to )
  152. {
  153. KCal::Alarm::List alarms;
  154. KCal::Journal::List notes = mCalendar.journals();
  155. KCal::Journal::List::ConstIterator note;
  156. for ( note = notes.begin(); note != notes.end(); ++note )
  157. {
  158. TQDateTime preTime = from.addSecs( -1 );
  159. KCal::Alarm::List::ConstIterator it;
  160. for( it = (*note)->alarms().begin(); it != (*note)->alarms().end(); ++it )
  161. {
  162. if ( (*it)->enabled() )
  163. {
  164. TQDateTime dt = (*it)->nextRepetition( preTime );
  165. if ( dt.isValid() && dt <= to )
  166. alarms.append( *it );
  167. }
  168. }
  169. }
  170. return alarms;
  171. }
  172. void ResourceXMLRPC::loginFinished( const TQValueList<TQVariant>& variant,
  173. const TQVariant& )
  174. {
  175. TQStringVariantMap map = variant[ 0 ].toMap();
  176. KURL url = KURL( mPrefs->url() );
  177. if ( map[ "GOAWAY" ].toString() == "XOXO" ) { // failed
  178. mSessionID = mKp3 = "";
  179. } else {
  180. mSessionID = map[ "sessionid" ].toString();
  181. mKp3 = map[ "kp3" ].toString();
  182. }
  183. url.setUser( mSessionID );
  184. url.setPass( mKp3 );
  185. mServer->setUrl( url );
  186. mSynchronizer->stop();
  187. }
  188. void ResourceXMLRPC::logoutFinished( const TQValueList<TQVariant>& variant,
  189. const TQVariant& )
  190. {
  191. TQStringVariantMap map = variant[ 0 ].toMap();
  192. if ( map[ "GOODBYE" ].toString() != "XOXO" )
  193. kdError() << "logout failed" << endl;
  194. KURL url = KURL( mPrefs->url() );
  195. mSessionID = mKp3 = "";
  196. url.setUser( mSessionID );
  197. url.setPass( mKp3 );
  198. mServer->setUrl( url );
  199. mSynchronizer->stop();
  200. }
  201. void ResourceXMLRPC::listNotesFinished( const TQValueList<TQVariant> &list, const TQVariant& )
  202. {
  203. TQMap<TQString, TQString>::Iterator uidIt;
  204. for ( uidIt = mUidMap.begin(); uidIt != mUidMap.end(); ++uidIt ) {
  205. KCal::Journal *journal = mCalendar.journal( uidIt.key() );
  206. mCalendar.deleteJournal( journal );
  207. }
  208. mUidMap.clear();
  209. TQValueList<TQVariant> noteList = list[ 0 ].toList();
  210. TQValueList<TQVariant>::Iterator noteIt;
  211. for ( noteIt = noteList.begin(); noteIt != noteList.end(); ++noteIt ) {
  212. TQStringVariantMap map = (*noteIt).toMap();
  213. KCal::Journal *journal = new KCal::Journal();
  214. TQString uid;
  215. readNote( map, journal, uid );
  216. mUidMap.insert( journal->uid(), uid );
  217. mCalendar.addJournal( journal );
  218. manager()->registerNote( this, journal );
  219. }
  220. mSynchronizer->stop();
  221. }
  222. void ResourceXMLRPC::addNoteFinished( const TQValueList<TQVariant> &list, const TQVariant &id )
  223. {
  224. int uid = list[ 0 ].toInt();
  225. mUidMap.insert( id.toString(), TQString::number( uid ) );
  226. mSynchronizer->stop();
  227. }
  228. void ResourceXMLRPC::updateNoteFinished( const TQValueList<TQVariant>&, const TQVariant& )
  229. {
  230. mSynchronizer->stop();
  231. }
  232. void ResourceXMLRPC::deleteNoteFinished( const TQValueList<TQVariant>&, const TQVariant &id )
  233. {
  234. mUidMap.erase( id.toString() );
  235. KCal::Journal *journal = mCalendar.journal( id.toString() );
  236. mCalendar.deleteJournal( journal );
  237. mSynchronizer->stop();
  238. }
  239. void ResourceXMLRPC::fault( int error, const TQString& errorMsg, const TQVariant& )
  240. {
  241. kdError() << "Server send error " << error << ": " << errorMsg << endl;
  242. mSynchronizer->stop();
  243. }
  244. void ResourceXMLRPC::writeNote( KCal::Journal* journal, TQStringVariantMap& args )
  245. {
  246. args.insert( "subject", journal->summary() );
  247. args.insert( "des", journal->description() );
  248. args.insert( "access",
  249. (journal->secrecy() == KCal::Journal::SecrecyPublic ? "public" : "private" ) );
  250. }
  251. void ResourceXMLRPC::readNote( const TQStringVariantMap& args, KCal::Journal *journal, TQString &uid )
  252. {
  253. uid = args[ "id" ].toString();
  254. journal->setSummary( args[ "subject" ].toString() );
  255. journal->setDescription( args[ "des" ].toString() );
  256. journal->setSecrecy( args[ "access" ].toString() == "public" ?
  257. KCal::Journal::SecrecyPublic : KCal::Journal::SecrecyPrivate );
  258. }
  259. #include "knotes_resourcexmlrpc.moc"