TDE core libraries
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.

kabapi.cc 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* -*- C++ -*-
  2. This file implements the application programming interface
  3. for using kab's addressbook files within other programs.
  4. Parse it with kdoc to get the API documentation.
  5. the TDE addressbook
  6. $ Author: Mirko Boehm $
  7. $ Copyright: (C) 1996-2001, Mirko Boehm $
  8. $ Contact: mirko@kde.org
  9. http://www.kde.org $
  10. $ License: GPL with the following explicit clarification:
  11. This code may be linked against any version of the Qt toolkit
  12. from Troll Tech, Norway. $
  13. $Id$
  14. */
  15. #include "kabapi.h"
  16. #include <tdelistbox.h>
  17. #include <tdemessagebox.h>
  18. #include <tdelocale.h>
  19. #include <kdebug.h>
  20. #include "kabapi.moc"
  21. #ifdef KAB_KDEBUG_AREA
  22. #undef KAB_KDEBUG_AREA
  23. #endif
  24. #define KAB_KDEBUG_AREA 800
  25. using namespace std;
  26. KabAPI::KabAPI(TQWidget* parent, const char* name)
  27. : KDialogBase(parent, name),
  28. book(0),
  29. listbox(new TDEListBox(this)),
  30. selection(-1)
  31. {
  32. TQ_CHECK_PTR(listbox);
  33. setMainWidget(listbox);
  34. showButtonApply(false);
  35. enableButtonSeparator(true);
  36. connect(listbox, TQT_SIGNAL(highlighted(int)), TQT_SLOT(entrySelected(int)));
  37. connect(listbox, TQT_SIGNAL(doubleClicked ( TQListBoxItem * )),TQT_SLOT(slotDoubleClicked ( TQListBoxItem * )));
  38. }
  39. void KabAPI::slotDoubleClicked ( TQListBoxItem * )
  40. {
  41. accept();
  42. }
  43. int KabAPI::exec()
  44. {
  45. TQStringList names;
  46. // -----
  47. if(book==0)
  48. {
  49. kdDebug(KAB_KDEBUG_AREA)
  50. << "KabAPI::exec: you have to call init before using the API."
  51. << endl;
  52. return -1;
  53. } else {
  54. if(book->getListOfNames(&names, true, false)==AddressBook::NoError)
  55. {
  56. listbox->clear();
  57. listbox->insertStringList(names);
  58. if(names.count()>0)
  59. {
  60. listbox->setCurrentItem(0);
  61. }
  62. listbox->setMinimumSize(listbox->sizeHint());
  63. adjustSize();
  64. resize(minimumSize());
  65. return KDialogBase::exec();
  66. } else {
  67. kdDebug(KAB_KDEBUG_AREA) << "KabAPI::exec: error creating interface."
  68. << endl;
  69. return -1;
  70. }
  71. }
  72. }
  73. AddressBook::ErrorCode KabAPI::init()
  74. {
  75. // ############################################################################
  76. book=new AddressBook(0, "KABAPI::book", true); //change parent from "this" to "0" //dsweet
  77. if(book->getState()==AddressBook::NoError)
  78. {
  79. connect(book, TQT_SIGNAL(setStatus(const TQString&)),
  80. TQT_SLOT(setStatusSlot(const TQString&)));
  81. return AddressBook::NoError;
  82. } else {
  83. return AddressBook::InternError;
  84. }
  85. // ############################################################################
  86. }
  87. AddressBook::ErrorCode KabAPI::getEntry(AddressBook::Entry& entry, KabKey& key)
  88. {
  89. // ############################################################################
  90. if(book->noOfEntries()==0)
  91. {
  92. return AddressBook::NoEntry;
  93. }
  94. if(selection>=0)
  95. {
  96. if(book->getKey(selection, key)==AddressBook::NoError)
  97. {
  98. if(book->getEntry(key, entry)==AddressBook::NoError)
  99. {
  100. return AddressBook::NoError;
  101. } else {
  102. return AddressBook::InternError; // this may not happen
  103. }
  104. } else {
  105. return AddressBook::NoEntry;
  106. }
  107. } else {
  108. return AddressBook::InternError;
  109. }
  110. // ############################################################################
  111. }
  112. AddressBook::ErrorCode KabAPI::add(const AddressBook::Entry& entry, KabKey& key,
  113. bool update)
  114. {
  115. // ############################################################################
  116. if(book->add(entry, key, update)!=AddressBook::NoError)
  117. {
  118. KMessageBox::sorry(this, i18n("Your new entry could not be added."));
  119. return AddressBook::InternError;
  120. } else {
  121. return AddressBook::NoError;
  122. }
  123. // ############################################################################
  124. }
  125. AddressBook::ErrorCode KabAPI::remove(const KabKey& key)
  126. {
  127. TQ_CHECK_PTR(book);
  128. // ############################################################################
  129. if(book->AddressBook::remove(key)==AddressBook::NoError)
  130. {
  131. return AddressBook::NoError;
  132. } else {
  133. return AddressBook::NoEntry;
  134. }
  135. // ############################################################################
  136. }
  137. AddressBook::ErrorCode KabAPI::getEntryByName(const TQString&,
  138. list<AddressBook::Entry>&, const int)
  139. {
  140. // ############################################################################
  141. return AddressBook::NotImplemented;
  142. // ############################################################################
  143. }
  144. AddressBook::ErrorCode KabAPI::getEntryByName(const AddressBook::Entry&,
  145. list<AddressBook::Entry>&, const int)
  146. {
  147. // ############################################################################
  148. return AddressBook::NotImplemented;
  149. // ############################################################################
  150. }
  151. AddressBook::ErrorCode KabAPI::getEntries(list<AddressBook::Entry>& entries)
  152. {
  153. kdDebug(KAB_KDEBUG_AREA) << "KabAPI::getEntries: called." << endl;
  154. // ############################################################################
  155. if(book->noOfEntries()==0)
  156. { // ----- database is valid, but empty:
  157. kdDebug(KAB_KDEBUG_AREA) << "KabAPI::getEntries: no entries." << endl;
  158. return AddressBook::NoEntry;
  159. }
  160. if(book->getEntries(entries)!=AddressBook::NoError)
  161. {
  162. kdDebug(KAB_KDEBUG_AREA) << "KabAPI::getEntries: intern error." << endl;
  163. return AddressBook::InternError;
  164. } else {
  165. kdDebug(KAB_KDEBUG_AREA) << "KabAPI::getEntries: done." << endl;
  166. return AddressBook::NoError;
  167. }
  168. // ############################################################################
  169. }
  170. AddressBook* KabAPI::addressbook()
  171. {
  172. // ############################################################################
  173. return book;
  174. // ############################################################################
  175. }
  176. AddressBook::ErrorCode KabAPI::save(bool force)
  177. {
  178. // ############################################################################
  179. if(book->save("", force)!=AddressBook::NoError)
  180. {
  181. return AddressBook::PermDenied;
  182. } else {
  183. return AddressBook::NoError;
  184. }
  185. // ############################################################################
  186. }
  187. void KabAPI::entrySelected(int index)
  188. {
  189. kdDebug(KAB_KDEBUG_AREA) << "KabAPI::entrySelected: entry " << index
  190. <<" selected." << endl;
  191. selection=index;
  192. }
  193. void KabAPI::setStatusSlot(const TQString& text)
  194. {
  195. emit(setStatus(text));
  196. }