From 072333880eb2346b33866f12336f5d27e67b85ec Mon Sep 17 00:00:00 2001
From: tcohen <tcohen@savoirfairelinux.net>
Date: Wed, 9 Dec 2009 15:40:03 -0500
Subject: [PATCH] [#2566] Begining of use of kapplication

---
 sflphone-client-kde/data/CMakeLists.txt       |   1 +
 .../data/sflphone-client-kde.notifyrc         |  25 ++++
 sflphone-client-kde/src/CMakeLists.txt        |   1 +
 sflphone-client-kde/src/SFLPhone.cpp          | 118 +++++++++++-----
 sflphone-client-kde/src/SFLPhone.h            |  11 +-
 .../src/SFLPhoneapplication.cpp               | 128 ++++++++++++++++++
 sflphone-client-kde/src/SFLPhoneapplication.h |  44 ++++++
 sflphone-client-kde/src/main.cpp              |  19 +--
 sflphone-client-kde/test.config               |   1 +
 9 files changed, 293 insertions(+), 55 deletions(-)
 create mode 100644 sflphone-client-kde/data/sflphone-client-kde.notifyrc
 create mode 100644 sflphone-client-kde/src/SFLPhoneapplication.cpp
 create mode 100644 sflphone-client-kde/src/SFLPhoneapplication.h
 create mode 100644 sflphone-client-kde/test.config

diff --git a/sflphone-client-kde/data/CMakeLists.txt b/sflphone-client-kde/data/CMakeLists.txt
index 99e05c78c1..4155adb86e 100644
--- a/sflphone-client-kde/data/CMakeLists.txt
+++ b/sflphone-client-kde/data/CMakeLists.txt
@@ -3,4 +3,5 @@ add_subdirectory(icons)
 
 INSTALL( FILES sflphone-client-kde.desktop  DESTINATION  ${XDG_APPS_INSTALL_DIR} )
 INSTALL( FILES sflphone-client-kdeui.rc  DESTINATION  ${DATA_INSTALL_DIR}/sflphone-client-kde )
+INSTALL( FILES sflphone-client-kde.notifyrc  DESTINATION ${DATA_INSTALL_DIR}/sflphone-client-kde)
 
diff --git a/sflphone-client-kde/data/sflphone-client-kde.notifyrc b/sflphone-client-kde/data/sflphone-client-kde.notifyrc
new file mode 100644
index 0000000000..48ceca1403
--- /dev/null
+++ b/sflphone-client-kde/data/sflphone-client-kde.notifyrc
@@ -0,0 +1,25 @@
+[Global]
+Name=sflphone-client-kde
+IconName=sflphone-client-kde
+Comment=SFLphone VoIP KDE4 client
+
+[Event/contact online]
+Name=Contact Online
+Comment=A contact has come online
+Sound=
+Action=Sound|Popup
+
+
+[Event/test_notification]
+Name=test message
+Comment=Test message
+Contexts=call
+Action=Sound|Popup|Taskbar
+Persistant=true
+Sound=
+
+[Event/test_notification2]
+Name=test message2
+Comment=Test message2
+Sound=KDE-Im-New-Mail.ogg
+Action=None
diff --git a/sflphone-client-kde/src/CMakeLists.txt b/sflphone-client-kde/src/CMakeLists.txt
index e1a056a105..765c46c23e 100644
--- a/sflphone-client-kde/src/CMakeLists.txt
+++ b/sflphone-client-kde/src/CMakeLists.txt
@@ -19,6 +19,7 @@ SET ( KDE4_KABC_LIBS  -lkabc )
 SET(	   sflphone_client_kde_SRCS
 	SFLPhoneView.cpp
 	SFLPhone.cpp
+        SFLPhoneapplication.cpp
 	main.cpp
 	sflphone_const.h
 	Account.cpp
diff --git a/sflphone-client-kde/src/SFLPhone.cpp b/sflphone-client-kde/src/SFLPhone.cpp
index d080f797db..7bfcf145cf 100644
--- a/sflphone-client-kde/src/SFLPhone.cpp
+++ b/sflphone-client-kde/src/SFLPhone.cpp
@@ -1,4 +1,4 @@
-/***************************************************************************
+/************************************** *************************************
  *   Copyright (C) 2009 by Savoir-Faire Linux                              *
  *   Author : Jérémy Quentin                                               *
  *   jeremy.quentin@savoirfairelinux.com                                   *
@@ -31,6 +31,7 @@
 #include <QtGui/QStatusBar>
 #include <QtGui/QCursor>
 #include <KActionCollection>
+#include <KNotification>
 
 #include "sflphone_const.h"
 #include "instance_interface_singleton.h"
@@ -39,43 +40,59 @@
 
 SFLPhone::SFLPhone(QWidget *parent)
     : KXmlGuiWindow(parent),
+      initialized_(false),
       view(new SFLPhoneView(this))
 {
-	
-	ConfigurationManagerInterface & configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
-	// accept dnd
-		setAcceptDrops(true);
-
-    // tell the KXmlGuiWindow that this is indeed the main widget
-		setCentralWidget(view);
-
-		setWindowIcon(QIcon(ICON_SFLPHONE));
-		setWindowTitle(i18n("SFLphone"));
-		
-		setupActions();
-		
-		setObjectNames();
-		QMetaObject::connectSlotsByName(this);
-	   view->on_stackedWidget_screen_currentChanged(SCREEN_MAIN);
-	   view->loadWindow();
-	   
-	   
-		move(QCursor::pos().x() - geometry().width()/2, QCursor::pos().y() - geometry().height()/2);
-	   if( ! configurationManager.isStartHidden())
-	   {
-	   	show();
-	   }
-	   
-	   if(configurationManager.getAccountList().value().isEmpty())
-		{
-			(new AccountWizard())->show();
-		}   
+  KNotification *notification = new KNotification( "contact online" );
+  notification->setText( "text" );
+  notification->sendEvent();
 } 
 
 SFLPhone::~SFLPhone()
 {
 }
 
+bool SFLPhone::initialize()
+{
+  if ( initialized_ )
+  {
+    qDebug() << "Already initialized.";
+    return false;
+  }
+
+ConfigurationManagerInterface & configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
+        // accept dnd
+                setAcceptDrops(true);
+
+    // tell the KXmlGuiWindow that this is indeed the main widget
+                setCentralWidget(view);
+
+                setWindowIcon(QIcon(ICON_SFLPHONE));
+                setWindowTitle(i18n("SFLphone"));
+
+                setupActions();
+
+                setObjectNames();
+                QMetaObject::connectSlotsByName(this);
+           view->on_stackedWidget_screen_currentChanged(SCREEN_MAIN);
+           view->loadWindow();
+
+
+                move(QCursor::pos().x() - geometry().width()/2, QCursor::pos().y() - geometry().height()/2);
+           if( ! configurationManager.isStartHidden())
+           {
+                show();
+           }
+
+           if(configurationManager.getAccountList().value().isEmpty())
+                {
+                        (new AccountWizard())->show();
+                }
+
+
+return true;
+}
+
 void SFLPhone::setObjectNames()
 {
 	view->setObjectName("view");
@@ -158,7 +175,8 @@ void SFLPhone::setupActions()
  	trayIconMenu = new QMenu(this);
  	trayIconMenu->addAction(action_quit);
 
-	trayIcon = new QSystemTrayIcon(this->windowIcon(), this);
+
+	trayIcon = new KSystemTrayIcon(this->windowIcon(), this);
 	trayIcon->setContextMenu(trayIconMenu);
 	trayIcon->show();
 	
@@ -224,11 +242,37 @@ void SFLPhone::trayIconSignal()
 
 void SFLPhone::sendNotif(QString caller)
 {
-	trayIcon->showMessage(
+    KComponentData m_instance = KGlobal::mainComponent();
+    KNotification::event("test_notification2", i18n("Feeds added:\n %1", "message"), QPixmap() , this, KNotification::CloseOnTimeout, m_instance);
+//--------------------------------
+    KNotification::event( "test_notification2",
+                          QString("summary"),
+                          QPixmap(),
+                          topLevelWidget());
+    KNotification::beep();
+
+//--------------------------------
+    notification = new KNotification ( QString("test_notification2"), 0l );
+    notification->setActions( QStringList( i18n( "View" ) ) );
+    notification->setText(QString("messageText"));
+    notification->sendEvent();
+//--------------------------------
+
+    notification = new KNotification ( QString("test_notification"), this );
+    notification->setText("messageText")    ;
+    notification->setPixmap( QPixmap( this->windowIcon().pixmap(32, 32) ));
+//    notification->setActions( QStringList( i18n( "Open chat" ) ) );
+    notification->addContext(  QString::fromLatin1("call") , "caller" )  ;
+//    connect(notification, SIGNAL(activated(unsigned int )), this , SLOT(sendNotif()) );
+    qDebug() << notification->contexts();
+    qDebug() << notification->eventId();
+    notification->sendEvent();
+//--------------------------------
+        trayIcon->showMessage(
 	    i18n("Incoming call"), 
 	    i18n("You have an incoming call from") + " " + caller + ".\n" + i18n("Click to accept or refuse it."), 
-	    QSystemTrayIcon::Warning, 
-	    20000);
+	    KSystemTrayIcon::Warning, 
+            20000);
 }
 
 void SFLPhone::on_trayIcon_messageClicked()
@@ -246,12 +290,12 @@ void SFLPhone::changeEvent(QEvent * event)
 	}
 }
 
-void SFLPhone::on_trayIcon_activated(QSystemTrayIcon::ActivationReason reason)
+void SFLPhone::on_trayIcon_activated(KSystemTrayIcon::ActivationReason reason)
 {
 	qDebug() << "on_trayIcon_activated";
 	switch (reason) {
-		case QSystemTrayIcon::Trigger:
-		case QSystemTrayIcon::DoubleClick:
+		case KSystemTrayIcon::Trigger:
+		case KSystemTrayIcon::DoubleClick:
 			qDebug() << "Tray icon clicked.";
 			if(isActiveWindow())
 			{
diff --git a/sflphone-client-kde/src/SFLPhone.h b/sflphone-client-kde/src/SFLPhone.h
index 1beae4fd10..0a8f749235 100644
--- a/sflphone-client-kde/src/SFLPhone.h
+++ b/sflphone-client-kde/src/SFLPhone.h
@@ -27,7 +27,8 @@
 #include <QtGui/QListWidgetItem>
 #include <QtGui/QKeyEvent>
 #include <QErrorMessage>
-#include <QSystemTrayIcon>
+#include <KSystemTrayIcon>
+#include <KNotification>
 
 #include <KXmlGuiWindow>
 #include <KAction>
@@ -70,6 +71,8 @@ enum CallAction {
         NumberOfCallActions};
 
 private:
+        // Whether or not the object has been initialized
+        bool   initialized_;
 	KAction * action_accept;
 	KAction * action_refuse;
 	KAction * action_hold;
@@ -90,7 +93,8 @@ private:
 	SFLPhoneView * view;
 	QMenu *trayIconMenu;
 	bool iconChanged;
-	QSystemTrayIcon *trayIcon;
+	KSystemTrayIcon *trayIcon;
+    KNotification *notification;
 	QLabel * statusBarWidget;
 	
 private:
@@ -104,6 +108,7 @@ protected:
 public:
 	SFLPhone(QWidget *parent = 0);
 	~SFLPhone();
+        bool initialize();
 	void setupActions();
 	void sendNotif(QString caller);
 	void putForeground();
@@ -113,7 +118,7 @@ public:
 	
 	
 private slots:
-	void on_trayIcon_activated(QSystemTrayIcon::ActivationReason reason);
+	void on_trayIcon_activated(KSystemTrayIcon::ActivationReason reason);
 	void on_trayIcon_messageClicked();
 	void on_view_statusMessageChangeAsked(const QString & message);
 	void on_view_windowTitleChangeAsked(const QString & message);
diff --git a/sflphone-client-kde/src/SFLPhoneapplication.cpp b/sflphone-client-kde/src/SFLPhoneapplication.cpp
new file mode 100644
index 0000000000..43b555dc97
--- /dev/null
+++ b/sflphone-client-kde/src/SFLPhoneapplication.cpp
@@ -0,0 +1,128 @@
+#include "SFLPhoneapplication.h"
+
+
+#include <KCmdLineArgs>
+#include <KIconLoader>
+#include <KStandardDirs>
+#include <KNotification>
+#include <KMainWindow>
+#include "SFLPhone.h"
+
+
+/**
+ * The application constructor
+ */
+SFLPhoneApplication::SFLPhoneApplication()
+  : KApplication()
+  , sflphoneWindow_(0)
+//  , quitSelected_(false)
+{
+  // SFLPhoneApplication is created from main.cpp.
+  // It continues the initialisation of the application.
+
+  // Install a message handler, so KMESS_ASSERT won't do a exit(1) or abort()
+  // It makes debugging output on Windows disappear, so don't use it there
+
+// test notif
+  KNotification *notification = new KNotification( "contact online" );
+  notification->setText( "text" );
+  notification->sendEvent();
+
+  // Start remaining initialisation
+//  initializePaths();
+//  initializeMainWindow();
+}
+
+
+
+/**
+ * Destructor
+ */
+SFLPhoneApplication::~SFLPhoneApplication()
+{
+  // automatically destroyed
+  sflphoneWindow_ = 0;
+}
+
+
+
+/**
+ * Return the sflphone window
+ */
+SFLPhone* SFLPhoneApplication::getSFLPhoneWindow() const
+{
+  return sflphoneWindow_;
+}
+
+
+/**
+ * Initialisation of the main window.
+ */
+void SFLPhoneApplication::initializeMainWindow()
+{
+  // Fetch the command line arguments
+  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
+
+  // Enable KDE session restore.
+  int restoredWindow = -1;
+  if( kapp->isSessionRestored() )
+  {
+    int n = 0;
+    while( KMainWindow::canBeRestored( ++n ) )
+    {
+      if( KMainWindow::classNameOfToplevel( n ) != QLatin1String( "SFLPhone" ) )
+      {
+        continue;
+      }
+
+      restoredWindow = n;
+      break;
+    }
+  }
+
+  // Create the main window and initialize it
+  sflphoneWindow_ = new SFLPhone();
+  if( ! sflphoneWindow_->initialize() )
+  {
+    exit(1);
+    return;
+  }
+
+  // Initialize KApplication
+  //setTopWidget( sflphoneWindow_ );
+
+  // We found session data for the Contact List, to restore it
+  if( kapp->isSessionRestored() && restoredWindow != -1 )
+  {
+    sflphoneWindow_->restore( restoredWindow, false );
+  }
+  else
+  {
+    if( ! args->isSet( "hidden" ) )
+    {
+      sflphoneWindow_->show();
+    }
+  }
+}
+
+
+
+/**
+ * Initialize additional paths
+ */
+void SFLPhoneApplication::initializePaths()
+{
+  // Add compile time paths as fallback
+  //KGlobal::dirs()       -> addPrefix( SFLPHONE_PREFIX );
+  //KIconLoader::global() -> addAppDir( SFLPHONE_PREFIX "/share" );
+
+  // Test whether the prefix is correct.
+  if( KGlobal::dirs()->findResource( "appdata", "icons/hi128-apps-sflphone-client-kde.png" ).isNull() )
+  {
+    kWarning() << "SFLPhone could not find resources in the search paths: "
+               << KGlobal::dirs()->findDirs( "appdata", QString::null ).join(", ") << endl;
+  }
+}
+
+
+#include "SFLPhoneapplication.moc"
diff --git a/sflphone-client-kde/src/SFLPhoneapplication.h b/sflphone-client-kde/src/SFLPhoneapplication.h
new file mode 100644
index 0000000000..38bd15c093
--- /dev/null
+++ b/sflphone-client-kde/src/SFLPhoneapplication.h
@@ -0,0 +1,44 @@
+#ifndef SFLPHONEAPPLICATION_H
+#define SFLPHONEAPPLICATION_H
+
+
+#include <KApplication>
+
+
+class SFLPhone;
+
+
+class SFLPhoneApplication : public KApplication
+{
+  Q_OBJECT
+
+  public:
+    // Constructor
+                SFLPhoneApplication();
+
+    // Destructor
+    virtual    ~SFLPhoneApplication();
+
+    // Return the contact list window
+    SFLPhone*       getSFLPhoneWindow() const;
+    // Return true if quit was selected
+    //bool         quitSelected() const;
+    // Tell the application that quit was selected
+    //void         setQuitSelected(bool quitSelected);
+
+ // private slots:
+ //void         slotAboutToQuit();
+ //   void         slotLastWindowClosed();
+
+  private:  // private methods
+    void         initializeMainWindow();
+    void         initializePaths();
+
+  private:
+    // Reference to the sflphone window
+    SFLPhone       *sflphoneWindow_;
+    // True when quit was selected
+    //bool         quitSelected_;
+};
+
+#endif // SFLPHONEAPPLICATION_H
diff --git a/sflphone-client-kde/src/main.cpp b/sflphone-client-kde/src/main.cpp
index 6be5f89959..3010809d98 100644
--- a/sflphone-client-kde/src/main.cpp
+++ b/sflphone-client-kde/src/main.cpp
@@ -4,16 +4,14 @@
 #include <QtGui/QMenuBar>
 #include <QtGui/QAction>
 
-#include <kapplication.h>
 #include <kcmdlineargs.h>
 #include <kaboutdata.h>
 #include <klocale.h>
 
-#include "SFLPhone.h"
 #include "AccountWizard.h"
 #include "instance_interface_singleton.h"
 #include "sflphone_const.h"
-
+#include "SFLPhoneapplication.h"
 #include "conf/ConfigurationDialog.h"
 
 #include <QTableView>
@@ -50,24 +48,15 @@ int main(int argc, char **argv)
 		//options.add("+[URL]", ki18n( "Document to open" ));
 		KCmdLineArgs::addCmdLineOptions(options);
 		
-		KApplication app;
-		
+                SFLPhoneApplication app;
+                return app.exec();
+
 		//configuration dbus
 		registerCommTypes();
-		new SFLPhone();
 
 		InstanceInterface & instance = InstanceInterfaceSingleton::getInstance();
 		instance.Register(getpid(), APP_NAME);
 		
-// 		SortableCodecListWidget * cl = new SortableCodecListWidget();
-// 		cl->show();
-
-// 		QListView * v = new QListView();
-// 		v->setFlow(QListView::TopToBottom);
-// 		v->setModel(new AccountListModel());
-// 		v->show();
-	
-		return app.exec();
 	}
 	catch(const char * msg)
 	{
diff --git a/sflphone-client-kde/test.config b/sflphone-client-kde/test.config
new file mode 100644
index 0000000000..8cec188b38
--- /dev/null
+++ b/sflphone-client-kde/test.config
@@ -0,0 +1 @@
+// ADD PREDEFINED MACROS HERE!
-- 
GitLab