[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kmail-devel
Subject:    [PATCH] UI abstraction proposal (partially only)
From:       Andreas Gungl <a.gungl () gmx ! de>
Date:       2003-06-02 22:00:32
[Download RAW message or body]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all,

attached you find a diff containing a first idea on how to separate the UI 
code from the processing code. It covers only the address book access in 
KMail.
I formed an abstract class for the UI callbacks, then I inherited a class 
covering the current Gui interactions.

I would like to have some comments on the patch, especially on naming 
conventions and related topics. E.g. the classes are named *GuiCallback, 
IMO it's better readable than *UiCallback, although text UIs are of course 
possible.
There is no factory class for the callbacks yet, although one could be very 
handy later on. Perhaps somebody with a better architectural overview over 
the sources can give a hint about where and how to integrate such a factory 
(e.g. new subdir?).

If there is real interest in such changes then I would try to work on this 
topic as much as my time allows. I can't make any promises for 3.2, but I 
could go step by step without breaking the application. If someone wants to 
join, you're welcome. Otherwise I've no problem to throw away the changes 
and come back later, when this issue has higher priority.

Regards,
Andreas
- -- 
    ~
  ' v '
 //   \\
/(     )\  Powered by Penguin.
  ^ ' ^

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux)

iD8DBQE+28kHVhjiFd4beU8RAgwjAKDIkU/Y12gRCqhNcbXS2fwvNkF8UQCfYXKh
s5UL8EZjwwitm3YxDcRv6PU=
=rwiX
-----END PGP SIGNATURE-----

["ui-abstraction.diff" (text/x-diff)]

Index: kmaddrbook.cpp
===================================================================
RCS file: /home/kde/kdepim/kmail/kmaddrbook.cpp,v
retrieving revision 1.69
diff -u -3 -p -u -b -r1.69 kmaddrbook.cpp
--- kmaddrbook.cpp	8 May 2003 09:10:44 -0000	1.69
+++ kmaddrbook.cpp	2 Jun 2003 21:36:52 -0000
@@ -20,6 +20,9 @@
 #include <kabc/stdaddressbook.h>
 #include <kabc/distributionlist.h>
 #include <kabc/vcardconverter.h>
+
+namespace KMail {
+
 void KabcBridge::addresses(QStringList& result) // includes lists
 {
   KCursorSaver busy(KBusyPtr::busy()); // loading might take a while
@@ -86,7 +89,6 @@ QStringList KabcBridge::addresses()
     return entries;
 }
 
-//-----------------------------------------------------------------------------
 QString KabcBridge::expandDistributionLists(const QString& recipients)
 {
   if (recipients.isEmpty())
@@ -166,10 +168,12 @@ QString KabcBridge::expandDistributionLi
   }
   return expRecipients;
 }
+
 //-----------------------------------------------------------------------------
-void KMAddrBookExternal::openEmail( const QString &addr, QWidget *) {
+void KMAddrBookExternal::openEmail( const QString &addr,
+                                    AddrBookGuiCallback *guiCallback ) {
   if (useKAddressbook()) {
-    if ( checkForAddressBook() ) {
+    if ( checkForAddressBook( guiCallback ) ) {
       KRun::runCommand( "kaddressbook -a " + KProcess::quote(addr) );
     }
     return;
@@ -179,8 +183,8 @@ void KMAddrBookExternal::openEmail( cons
   // silently to kabc.
 }
 
-//-----------------------------------------------------------------------------
-void KMAddrBookExternal::addEmail( const QString& addr, QWidget *parent) {
+void KMAddrBookExternal::addEmail( const QString& addr,
+                                   AddrBookGuiCallback *guiCallback ) {
   QString email;
   QString name;
 
@@ -198,27 +202,22 @@ void KMAddrBookExternal::addEmail( const
     ab->insertAddressee(a);
 
     if ( !KABC::StdAddressBook::save() ) {
-      KMessageBox::error( parent, i18n("Can't save to addressbook.") );
+      guiCallback->errorSavingToAddrBook();
     } else {
-      QString text = i18n("<qt>The email address <b>%1</b> was added to your "
-                          "addressbook. You can add more information to this "
-                          "entry by opening the addressbook.</qt>").arg( addr );
-      KMessageBox::information( parent, text, QString::null, "addedtokabc" );
+      guiCallback->savedAddress( addr );
     }
   } else {
-    QString text = i18n("<qt>The email address <b>%1</b> is already in your "
-                        "addressbook.</qt>").arg( addr );
-    KMessageBox::information( parent, text );
+    guiCallback->foundExistingAddress( addr );
   }
 }
 
-void KMAddrBookExternal::launch(QWidget *) {
+void KMAddrBookExternal::launch( AddrBookGuiCallback *guiCallback ) {
 // Reenable selection, when kab is ported to libkabc.
 // It might be better to remove the useK* functions and to check the config
 // file directly, as they aren't used anywhere else.
 // It might also be better to write a string for identifying the addressbook
 // instead of a number as it is done now.
-  if ( checkForAddressBook() ) {
+  if ( checkForAddressBook( guiCallback ) ) {
     KRun::runCommand("kaddressbook");
   }
 #if 0
@@ -248,22 +247,20 @@ bool KMAddrBookExternal::useKAddressbook
   return (ab == 1);
 }
 
-bool KMAddrBookExternal::checkForAddressBook()
+bool KMAddrBookExternal::checkForAddressBook( AddrBookGuiCallback *guiCallback )
 {
   if ( KStandardDirs::findExe( "kaddressbook" ).isEmpty() ) {
-    KMessageBox::information( 0,
-        i18n("No external address book application found. You might want to "
-             "install KAddressBook from the kdepim module.") );
+    guiCallback->noAddrBookFound();
     return false;
   } else {
     return true;
   }
 }
 
-void KMAddrBookExternal::addNewAddressee( QWidget* )
+void KMAddrBookExternal::addNewAddressee( AddrBookGuiCallback *guiCallback )
 {
   if (useKAddressbook()) {
-    if ( checkForAddressBook() ) {
+    if ( checkForAddressBook( guiCallback ) ) {
       KRun::runCommand( "kaddressbook --editor-only --new-contact" );
     }
     return;
@@ -272,7 +269,8 @@ void KMAddrBookExternal::addNewAddressee
   // silently to kabc.
 }
 
-bool KMAddrBookExternal::addVCard( const KABC::Addressee& addressee, QWidget *parent )
+bool KMAddrBookExternal::addVCard( const KABC::Addressee& addressee,
+                                   AddrBookGuiCallback *guiCallback )
 {
   KABC::AddressBook *ab = KABC::StdAddressBook::self();
   bool inserted = false;
@@ -283,23 +281,74 @@ bool KMAddrBookExternal::addVCard( const
   if ( addressees.isEmpty() ) {
     ab->insertAddressee( addressee );
     if ( !KABC::StdAddressBook::save() ) {
-      KMessageBox::error( parent, i18n("Can't save to addressbook.") );
+      guiCallback->errorSavingToAddrBook();
       inserted = false;
     } else {
-      QString text = i18n("The VCard was added to your addressbook. "
-                          "You can add more information to this "
-                          "entry by opening the addressbook.");
-      KMessageBox::information( parent, text, QString::null, "addedtokabc" );
+      guiCallback->savedVCard();
       inserted = true;
     }
   } else {
-    QString text = i18n("The VCard's primary email address is already in "
-                        "your addressbook. However you may save the VCard "
-                        "into a file and import it into the addressbook "
-                        "manually.");
-    KMessageBox::information( parent, text );
+    guiCallback->foundExistingVCard();
     inserted = true;
   }
 
   return inserted;
 }
+
+//-----------------------------------------------------------------------------
+AddrBookGuiCallback::AddrBookGuiCallback( QWidget *parent )
+  : mParent( parent )
+{
+}
+
+//-----------------------------------------------------------------------------
+AddrBookDefaultGuiCallback::AddrBookDefaultGuiCallback( QWidget *parent )
+  : AddrBookGuiCallback( parent )
+{
+}
+
+void AddrBookDefaultGuiCallback::noAddrBookFound()
+{
+  QString text = i18n("No external address book application found. You might "
+                      "want to install KAddressBook from the kdepim module.");
+  KMessageBox::information( mParent, text );
+}
+
+void AddrBookDefaultGuiCallback::errorSavingToAddrBook()
+{
+  KMessageBox::error( mParent, i18n("Can't save to addressbook.") );
+}
+
+void AddrBookDefaultGuiCallback::savedAddress( QString email )
+{
+  QString text = i18n("<qt>The email address <b>%1</b> was added to your "
+                      "addressbook. You can add more information to this "
+                      "entry by opening the addressbook.</qt>").arg( email );
+  KMessageBox::information( mParent, text, QString::null, "addedtokabc" );
+}
+
+void AddrBookDefaultGuiCallback::foundExistingAddress( QString email )
+{
+  QString text = i18n("<qt>The email address <b>%1</b> is already in your "
+                      "addressbook.</qt>").arg( email );
+  KMessageBox::information( mParent, text );
+}
+
+void AddrBookDefaultGuiCallback::savedVCard()
+{
+  QString text = i18n("The VCard was added to your addressbook. "
+                      "You can add more information to this "
+                      "entry by opening the addressbook.");
+  KMessageBox::information( mParent, text, QString::null, "addedtokabc" );
+}
+
+void AddrBookDefaultGuiCallback::foundExistingVCard()
+{
+  QString text = i18n("The VCard's primary email address is already in "
+                      "your addressbook. However you may save the VCard "
+                      "into a file and import it into the addressbook "
+                      "manually.");
+  KMessageBox::information( mParent, text );
+}
+
+}; // namespace KMail
Index: kmaddrbook.h
===================================================================
RCS file: /home/kde/kdepim/kmail/kmaddrbook.h,v
retrieving revision 1.20
diff -u -3 -p -u -b -r1.20 kmaddrbook.h
--- kmaddrbook.h	8 May 2003 09:10:44 -0000	1.20
+++ kmaddrbook.h	2 Jun 2003 21:36:52 -0000
@@ -2,8 +2,8 @@
  * Author: Stefan Taferner <taferner@kde.org>
  * This code is under GPL
  */
-#ifndef KMAddrBook_h
-#define KMAddrBook_h
+#ifndef __KMAIL_KMADDRBOOK_H
+#define __KMAIL_KMADDRBOOK_H
 
 #include <qstringlist.h>
 #include <qwidget.h>
@@ -11,6 +11,10 @@
 #include <kdeversion.h>
 #include <kabc/addressee.h>
 
+namespace KMail {
+
+class AddrBookGuiCallback;
+
 class KabcBridge {
 public:
   static QStringList addresses();
@@ -20,14 +24,41 @@ public:
 
 class KMAddrBookExternal {
 public:
-  static void addEmail( const QString &addr, QWidget *parent );
-  static void addNewAddressee( QWidget* );
-  static void openEmail( const QString &addr, QWidget *parent );
-  static void launch( QWidget *parent );
+  static void addEmail( const QString &addr, AddrBookGuiCallback *guiCallback );
+  static void addNewAddressee( AddrBookGuiCallback *guiCallback );
+  static void openEmail( const QString &addr, AddrBookGuiCallback *guiCallback );
+  static void launch( AddrBookGuiCallback *guiCallback );
   static bool useKab();
   static bool useKAddressbook();
-  static bool checkForAddressBook();
-  static bool addVCard( const KABC::Addressee& addressee, QWidget *parent );
+  static bool checkForAddressBook( AddrBookGuiCallback *guiCallback );
+  static bool addVCard( const KABC::Addressee& addressee,
+                        AddrBookGuiCallback *guiCallback );
+};
+
+class AddrBookGuiCallback {
+public:
+  AddrBookGuiCallback( QWidget *parent );
+  virtual void noAddrBookFound() = 0;
+  virtual void errorSavingToAddrBook() = 0;
+  virtual void savedAddress( QString email ) = 0;
+  virtual void foundExistingAddress( QString email ) = 0;
+  virtual void savedVCard() = 0;
+  virtual void foundExistingVCard() = 0;
+protected:
+  QWidget *mParent;
 };
 
-#endif /*KMAddrBook_h*/
+class AddrBookDefaultGuiCallback : public AddrBookGuiCallback {
+public:
+  AddrBookDefaultGuiCallback( QWidget *parent );
+  virtual void noAddrBookFound();
+  virtual void errorSavingToAddrBook();
+  virtual void savedAddress( QString email );
+  virtual void foundExistingAddress( QString email );
+  virtual void savedVCard();
+  virtual void foundExistingVCard();
+};
+
+}; // namespace KMail
+
+#endif // __KMAIL_KMADDRBOOK_H
Index: kmcommands.cpp
===================================================================
RCS file: /home/kde/kdepim/kmail/kmcommands.cpp,v
retrieving revision 1.49
diff -u -3 -p -u -b -r1.49 kmcommands.cpp
--- kmcommands.cpp	2 Jun 2003 19:05:33 -0000	1.49
+++ kmcommands.cpp	2 Jun 2003 21:36:52 -0000
@@ -41,6 +41,9 @@
 #include <kstandarddirs.h>
 #include "mailinglist-magic.h"
 #include "kmaddrbook.h"
+using KMail::KMAddrBookExternal;
+using KMail::AddrBookGuiCallback;
+using KMail::AddrBookDefaultGuiCallback;
 #include "kmcomposewin.h"
 #include "kmfiltermgr.h"
 #include "kmfolderimap.h"
@@ -378,7 +381,9 @@ KMMailtoAddAddrBookCommand::KMMailtoAddA
 
 void KMMailtoAddAddrBookCommand::execute()
 {
-  KMAddrBookExternal::addEmail( KMMessage::decodeMailtoUrl( mUrl.path() ), mParent );
+  AddrBookGuiCallback *guiCallback = new AddrBookDefaultGuiCallback( mParent );
+  KMAddrBookExternal::addEmail( KMMessage::decodeMailtoUrl( mUrl.path() ), guiCallback );
+  delete guiCallback;
 }
 
 
@@ -390,7 +395,9 @@ KMMailtoOpenAddrBookCommand::KMMailtoOpe
 
 void KMMailtoOpenAddrBookCommand::execute()
 {
-  KMAddrBookExternal::openEmail( KMMessage::decodeMailtoUrl( mUrl.path() ), mParent );
+  AddrBookGuiCallback *guiCallback = new AddrBookDefaultGuiCallback( mParent );
+  KMAddrBookExternal::openEmail( KMMessage::decodeMailtoUrl( mUrl.path() ), guiCallback );
+  delete guiCallback;
 }
 
 
Index: kmcomposewin.cpp
===================================================================
RCS file: /home/kde/kdepim/kmail/kmcomposewin.cpp,v
retrieving revision 1.700
diff -u -3 -p -u -b -r1.700 kmcomposewin.cpp
--- kmcomposewin.cpp	2 Jun 2003 10:44:02 -0000	1.700
+++ kmcomposewin.cpp	2 Jun 2003 21:36:56 -0000
@@ -24,6 +24,10 @@
 #include "kmmsgpartdlg.h"
 #include <kpgpblock.h>
 #include "kmaddrbook.h"
+using KMail::KabcBridge;
+using KMail::KMAddrBookExternal;
+using KMail::AddrBookGuiCallback;
+using KMail::AddrBookDefaultGuiCallback;
 #include "kmmsgdict.h"
 #include "kmfolderimap.h"
 #include "kmfoldermgr.h"
@@ -3901,7 +3905,9 @@ void KMComposeWin::setCharset(const QCSt
 //-----------------------------------------------------------------------------
 void KMComposeWin::slotAddrBook()
 {
-  KMAddrBookExternal::launch(this);
+  AddrBookGuiCallback *guiCallback = new AddrBookDefaultGuiCallback( this );
+  KMAddrBookExternal::launch(guiCallback);
+  delete guiCallback;
 }
 
 
Index: kmmainwidget.cpp
===================================================================
RCS file: /home/kde/kdepim/kmail/kmmainwidget.cpp,v
retrieving revision 1.54
diff -u -3 -p -u -b -r1.54 kmmainwidget.cpp
--- kmmainwidget.cpp	25 May 2003 22:22:00 -0000	1.54
+++ kmmainwidget.cpp	2 Jun 2003 21:36:57 -0000
@@ -40,6 +40,9 @@
 #include "kmfiltermgr.h"
 #include "kmsender.h"
 #include "kmaddrbook.h"
+using KMail::KMAddrBookExternal;
+using KMail::AddrBookGuiCallback;
+using KMail::AddrBookDefaultGuiCallback;
 #include "kmversion.h"
 #include "kmfldsearch.h"
 #include "kmacctfolder.h"
@@ -802,7 +805,9 @@ void KMMainWidget::slotPopFilter()
 //-----------------------------------------------------------------------------
 void KMMainWidget::slotAddrBook()
 {
-  KMAddrBookExternal::launch(this);
+  AddrBookGuiCallback *guiCallback = new AddrBookDefaultGuiCallback( this );
+  KMAddrBookExternal::launch( guiCallback );
+  delete guiCallback;
 }
 
 
Index: vcardviewer.cpp
===================================================================
RCS file: /home/kde/kdepim/kmail/vcardviewer.cpp,v
retrieving revision 1.2
diff -u -3 -p -u -b -r1.2 vcardviewer.cpp
--- vcardviewer.cpp	7 May 2003 22:07:25 -0000	1.2
+++ vcardviewer.cpp	2 Jun 2003 21:36:57 -0000
@@ -67,8 +67,10 @@ VCardViewer::~VCardViewer()
 
 void VCardViewer::slotUser1()
 {
-  if (KMAddrBookExternal::addVCard(mAddressee, this))
+  AddrBookGuiCallback *guiCallback = new AddrBookDefaultGuiCallback( this );
+  if (KMAddrBookExternal::addVCard(mAddressee, guiCallback))
     showButton(User1, false);
+  delete guiCallback;
 }
 
 #include "vcardviewer.moc"


_______________________________________________
KMail Developers mailing list
kmail@mail.kde.org
http://mail.kde.org/mailman/listinfo/kmail


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic