From 46019189dc5ab3d70f2f3a686343a113cc8a1888 Mon Sep 17 00:00:00 2001
From: yanmorin <yanmorin>
Date: Fri, 1 Sep 2006 18:56:20 +0000
Subject: [PATCH] add launcher improvment and window movement

---
 ChangeLog                           |  6 ++++++
 src/gui/qt/ConfigurationPanel.ui.h  |  6 +++---
 src/gui/qt/Makefile.am              |  1 +
 src/gui/qt/NumericKeypad.cpp        | 10 ++++++----
 src/gui/qt/NumericKeypad.hpp        |  2 ++
 src/gui/qt/PhoneLineManagerImpl.cpp |  8 +++++++-
 src/gui/qt/PhoneLineManagerImpl.hpp |  3 +++
 src/gui/qt/Request.cpp              | 12 ++++++------
 src/gui/qt/SFLPhoneApp.cpp          |  7 +++++--
 src/gui/qt/SFLPhoneApp.hpp          |  4 +++-
 src/gui/qt/SFLPhoneWindow.cpp       |  6 ++++++
 src/gui/qt/Session.cpp              |  5 +++--
 src/gui/qt/Session.hpp              | 13 ++++++++++---
 src/gui/qt/SessionIO.hpp            |  6 ++++--
 src/gui/qt/TCPSessionIO.cpp         | 24 +++++++++++++++---------
 src/gui/qt/TCPSessionIO.hpp         |  5 +++--
 src/gui/qt/main.cpp                 |  1 -
 17 files changed, 83 insertions(+), 36 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 8b9ffdbb9c..9f67fdd0f3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2006-08-31  Yan Morin
+
+	* Improve launcher (only start if needed). If the daemon is started, qt won't stop it.
+
+	* Fix buf with microphone (if the mic wasn't at 100, it causes a segfault before)
+
 2006-08-31  Yan Morin
 
 	* Add samplerate combobox if sample rate is compiled
diff --git a/src/gui/qt/ConfigurationPanel.ui.h b/src/gui/qt/ConfigurationPanel.ui.h
index f56366a069..78b34a7e45 100644
--- a/src/gui/qt/ConfigurationPanel.ui.h
+++ b/src/gui/qt/ConfigurationPanel.ui.h
@@ -52,7 +52,7 @@
 void ConfigurationPanel::init()
 {
   _cutStringCombo = 30;
-  DebugOutput::instance() << "ConfigurationPanel::init()\n"; 
+  //DebugOutput::instance() << "ConfigurationPanel::init()\n"; 
   lblError->hide();
   Tab_Signalisations->show();
   Tab_Audio->hide();
@@ -339,7 +339,7 @@ void ConfigurationPanel::updateAudioDevices()
     if (hostApiName.length() > _cutStringCombo) {
 	    hostApiName = hostApiName.left(_cutStringCombo) + "...";
     }
-    DebugOutput::instance() << hostApiName << pos->defaultRate;
+    //DebugOutput::instance() << hostApiName << pos->defaultRate;
     QString name = hostApiName + QObject::tr(" (device #%1-%2Hz)").arg(pos->index).arg(pos->defaultRate);
     cbo->insertItem(name);
   }
@@ -421,7 +421,7 @@ ConfigurationPanel::slotSIPAccountChange(int index)
   if (lastSIPAccount!=index) {
     
     QString account = "SIP" + QString::number(index);
-    DebugOutput::instance() << "Selecting SIP account " << account << "\n";
+    //DebugOutput::instance() << "Selecting SIP account " << account << "\n";
 
     saveSIPAccount(lastSIPAccount);
     loadSIPAccount(index);
diff --git a/src/gui/qt/Makefile.am b/src/gui/qt/Makefile.am
index 5fa20d1e35..9403bc30ae 100644
--- a/src/gui/qt/Makefile.am
+++ b/src/gui/qt/Makefile.am
@@ -17,6 +17,7 @@ BUILT_SOURCES = \
     ./SFLPhoneWindowmoc.cpp \
     ./SFLRequestmoc.cpp \
     ./Requestmoc.cpp \
+    ./Sessionmoc.cpp \
     ./SessionIOmoc.cpp \
     ./TCPSessionIOmoc.cpp \
     ./TransparentWidgetmoc.cpp \
diff --git a/src/gui/qt/NumericKeypad.cpp b/src/gui/qt/NumericKeypad.cpp
index ea90916062..8ded67b23f 100644
--- a/src/gui/qt/NumericKeypad.cpp
+++ b/src/gui/qt/NumericKeypad.cpp
@@ -237,15 +237,16 @@ NumericKeypad::mouseMoveEvent(QMouseEvent *e)
 //  DebugOutput::instance() << "mWinRef (x1,y1): " << wx1 << " " << wy1 << "\n";  
 
     // x and y
-    if      (abs(px0-wx1) <= range) { pt.setX(wx1); }
-    else if (abs(px1-wx0) <= range) { pt.setX(wx0-width()); }
+    mIsDock = false;
+    if      (abs(px0-wx1) <= range) { pt.setX(wx1); mIsDock = true; }
+    else if (abs(px1-wx0) <= range) { pt.setX(wx0-width()); mIsDock = true; }
 
     // top and down
     if      (abs(py0-wy0) <= range)  { pt.setY(wy0); }
     // the numeric under the telephone
-    else if (abs(py0-wy1) <= range) { pt.setY(wy1); }
+    else if (abs(py0-wy1) <= range) { pt.setY(wy1); mIsDock = true;}
     // the numeric over the telephone
-    else if (abs(py1-wy0) <= range) { pt.setY(wy0-height()); }
+    else if (abs(py1-wy0) <= range) { pt.setY(wy0-height()); mIsDock = true;}
   }
 
   move(pt);
@@ -311,6 +312,7 @@ void
 NumericKeypad::setDefaultPosition(const QPoint& point) {
   if (mWinRef && !mAlreadySet) {
     move(point);
+    mIsDock = true;
     mAlreadySet = true;
   }
 }
diff --git a/src/gui/qt/NumericKeypad.hpp b/src/gui/qt/NumericKeypad.hpp
index b996df1655..354e73db30 100644
--- a/src/gui/qt/NumericKeypad.hpp
+++ b/src/gui/qt/NumericKeypad.hpp
@@ -53,6 +53,7 @@ public:
   /** Set default position of the window */
   void setDefaultPosition(const QPoint&);
   void setWindowReference(QWidget* widget) { mWinRef = widget; }
+  bool isDock() { return mIsDock; }
 
 public slots:
   void mousePressEvent(QMouseEvent *e);
@@ -84,6 +85,7 @@ private:
   /** Window reference when moving the window (magnetic style) */
   QWidget* mWinRef;
   bool mAlreadySet; // already set the default position or not?
+  bool mIsDock; // beside the ref window or not?
 };
 
 #endif // __NUMERIC_KEYPAD_H__
diff --git a/src/gui/qt/PhoneLineManagerImpl.cpp b/src/gui/qt/PhoneLineManagerImpl.cpp
index bc14254a28..916a1f6f19 100644
--- a/src/gui/qt/PhoneLineManagerImpl.cpp
+++ b/src/gui/qt/PhoneLineManagerImpl.cpp
@@ -44,6 +44,7 @@ PhoneLineManagerImpl::PhoneLineManagerImpl()
   , mMicVolume(-1)
   , mIsConnected(false)
   , mIsStopping(false)
+  , mShouldStopDaemon(false)
   , mLastNumber("")
 {
   EventFactory::instance().registerDefaultEvent< DefaultEvent >();
@@ -191,7 +192,12 @@ PhoneLineManagerImpl::stop()
   emit globalStatusSet(QString(tr("Stopping sflphone server..")));
   mIsStopping = true;
   if(mIsConnected) {
-    mSession->stop();
+    if (mShouldStopDaemon) {
+      mSession->stop();
+      emit stopped();
+    } else {
+      mSession->quit();
+    }
   }
   else {
     emit stopped();
diff --git a/src/gui/qt/PhoneLineManagerImpl.hpp b/src/gui/qt/PhoneLineManagerImpl.hpp
index a1b33429a6..2afc5f73fa 100644
--- a/src/gui/qt/PhoneLineManagerImpl.hpp
+++ b/src/gui/qt/PhoneLineManagerImpl.hpp
@@ -62,11 +62,13 @@ public:
   PhoneLine *getCurrentLine();
 
   void setNbLines(unsigned int line);
+  void shouldStopDaemon(bool shouldStop) { mShouldStopDaemon  = shouldStop; }
 
   bool isConnected() { return mIsConnected; }
   void emitReadyAccount() { emit readyToGetAccount(); }
   void emitReadyToShow() { emit readyToShow(); }
   void addAccount(const QString& name, bool isEnabled, const QString& alias);   
+
 signals:
   void unselected(unsigned int);
   void selected(unsigned int);
@@ -352,6 +354,7 @@ private:
 
   bool mIsConnected;
   bool mIsStopping;
+  bool mShouldStopDaemon;
 
   QString mLastNumber;
 };
diff --git a/src/gui/qt/Request.cpp b/src/gui/qt/Request.cpp
index 5592543f0a..6371174d52 100644
--- a/src/gui/qt/Request.cpp
+++ b/src/gui/qt/Request.cpp
@@ -110,12 +110,12 @@ Request::onEntry(const QString &code, const QString &message)
 void
 Request::onSuccess(const QString &code, const QString &message)
 {
-  DebugOutput::instance() << QObject::tr("Received a success info:  "
-					 "Code/SeqID: %1/%2\t"
-					 "Message: %3\n")
-    .arg(code)
-    .arg(mSequenceId)
-    .arg(message);
+//  DebugOutput::instance() << QObject::tr("Received a success info:  "
+//					 "Code/SeqID: %1/%2\t"
+//					 "Message: %3\n")
+//    .arg(code)
+//    .arg(mSequenceId)
+//    .arg(message);
   QString messageDecoded = message;
   Url::decode(messageDecoded);
 
diff --git a/src/gui/qt/SFLPhoneApp.cpp b/src/gui/qt/SFLPhoneApp.cpp
index 0dcc008460..413c14d946 100644
--- a/src/gui/qt/SFLPhoneApp.cpp
+++ b/src/gui/qt/SFLPhoneApp.cpp
@@ -47,11 +47,14 @@ SFLPhoneApp::SFLPhoneApp(int argc, char **argv)
   SessionIOFactory::instance().setCreator(new TCPSessionIOCreator(QString("localhost"), 3999));
 
   mSession = new Session();
+  connect(mSession, SIGNAL(firstSessionIOConnectionFailed()), this, SLOT(launch()));
 
   ConfigurationManager::instance().setSession(mSession);
 
   PhoneLineManager::instance().initialize(mSession);
   PhoneLineManager::instance().setNbLines(NB_PHONELINES);
+  PhoneLineManager::instance().shouldStopDaemon(false);
+
   Requester::instance().registerDefaultObject< Request >();
   Requester::instance().registerObject< Request >(QString("playtone"));
   Requester::instance().registerObject< Request >(QString("stoptone"));
@@ -97,6 +100,7 @@ void
 SFLPhoneApp::launch()
 {
    if(mLauncher) {
+     PhoneLineManager::instance().shouldStopDaemon(true);
      mLauncher->start();
    }
 }
@@ -206,8 +210,7 @@ SFLPhoneApp::initConnections(SFLPhoneWindow *w)
   QObject::connect(&PhoneLineManager::instance(), SIGNAL(readyToShow()), w, SLOT(show()));
   QObject::connect(w, SIGNAL(reconnectAsked()),
 		   &PhoneLineManager::instance(), SLOT(connect()));
-  QObject::connect(&PhoneLineManager::instance(), SIGNAL(stopped()),
-		   w, SLOT(close()));
+  QObject::connect(&PhoneLineManager::instance(), SIGNAL(stopped()), w, SLOT(close()));
   QObject::connect(w, SIGNAL(needToCloseDaemon()),
 		   &PhoneLineManager::instance(), SLOT(stop()));
 
diff --git a/src/gui/qt/SFLPhoneApp.hpp b/src/gui/qt/SFLPhoneApp.hpp
index 3fd6b9f078..f2382f1b7a 100644
--- a/src/gui/qt/SFLPhoneApp.hpp
+++ b/src/gui/qt/SFLPhoneApp.hpp
@@ -50,7 +50,6 @@ public:
   void initConnections(SFLPhoneWindow *w);
   void loadSkin();
 
-  void launch();
 
 public slots:
   /**
@@ -60,6 +59,9 @@ public slots:
   void paste();
   void shortcutPressed(QKeyEvent* e);
 
+private slots:
+  void launch();
+
 signals:
   void registerFailed(const QString);
   void registerSucceed(const QString);
diff --git a/src/gui/qt/SFLPhoneWindow.cpp b/src/gui/qt/SFLPhoneWindow.cpp
index 0903e7846b..431af1fde4 100644
--- a/src/gui/qt/SFLPhoneWindow.cpp
+++ b/src/gui/qt/SFLPhoneWindow.cpp
@@ -268,7 +268,13 @@ void
 SFLPhoneWindow::delayedPaint()
 {
   if(pos() != mLastWindowPos) {
+    if (mLastWindowPos.x() < 0) { mLastWindowPos.setX(0); }
+    QPoint diff = mLastWindowPos - pos();
     move(mLastWindowPos);
+    if (mKeypad && mKeypad->isDock()) {
+      // Use (mKeypad->pos() - diff) for a cool effects
+      mKeypad->move(mKeypad->pos() + diff);
+    }
   }
 }
 
diff --git a/src/gui/qt/Session.cpp b/src/gui/qt/Session.cpp
index fb989f37c5..b916833337 100644
--- a/src/gui/qt/Session.cpp
+++ b/src/gui/qt/Session.cpp
@@ -38,6 +38,7 @@ Session::Session()
 {
   mId = Requester::instance().generateSessionId();
   SessionIO *s = SessionIOFactory::instance().create();
+  QObject::connect(s, SIGNAL(firstConnectionFailed()), this, SIGNAL(firstSessionIOConnectionFailed()));
   Requester::instance().registerSession(mId, s);
 }
 
@@ -109,9 +110,9 @@ Session::configSave() const
 }
 
 Request *
-Session::close() const
+Session::quit() const
 {
-  return Requester::instance().send(mId, "close", std::list< QString >());
+  return Requester::instance().send(mId, "quit", std::list< QString >());
 }
 
 Request *
diff --git a/src/gui/qt/Session.hpp b/src/gui/qt/Session.hpp
index 6996ad62f3..1b2f2c4d62 100644
--- a/src/gui/qt/Session.hpp
+++ b/src/gui/qt/Session.hpp
@@ -22,17 +22,20 @@
 #ifndef SFLPHONEGUI_SESSION_H
 #define SFLPHONEGUI_SESSION_H
 
+#include <qobject.h>
 #include <qstring.h>
 #include <map>
 
 #include "Account.hpp"
 
-class Session
+class Session : public QObject
 {
+  Q_OBJECT
+
  public:
   Session();
   Session(const QString &id);
-  ~Session();
+  virtual ~Session();
   
   /**
    * retreive the account identified by name.
@@ -91,7 +94,7 @@ class Session
    * the session. This will only close the session,
    * so sflphoned will still be running after.
    */
-  Request *close() const;
+  Request *quit() const;
 
   /**
    * This function will register with the default account.
@@ -139,6 +142,10 @@ class Session
   Request *stopTone() const;
   Request *playTone() const;
 
+ signals:
+  void firstSessionIOConnectionFailed(); 
+
+
  private:
   QString mId;
   QString mSelectedAccountId;
diff --git a/src/gui/qt/SessionIO.hpp b/src/gui/qt/SessionIO.hpp
index e845e8e00c..21d2fc89c3 100644
--- a/src/gui/qt/SessionIO.hpp
+++ b/src/gui/qt/SessionIO.hpp
@@ -1,4 +1,4 @@
-/**
+/*
  *  Copyright (C) 2004-2005 Savoir-Faire Linux inc.
  *  Author: Jean-Philippe Barrette-LaPierre
  *             <jean-philippe.barrette-lapierre@savoirfairelinux.com>
@@ -32,7 +32,7 @@ class SessionIO : public QObject
 {
   Q_OBJECT
   
- public:
+public:
   virtual ~SessionIO(){}
 
 public slots:
@@ -53,6 +53,8 @@ public slots:
    */
   virtual void receive(QString &answer) = 0;
 
+signals:
+  void firstConnectionFailed();
 };
 
 
diff --git a/src/gui/qt/TCPSessionIO.cpp b/src/gui/qt/TCPSessionIO.cpp
index 85e8f830a0..e9f203fc3b 100644
--- a/src/gui/qt/TCPSessionIO.cpp
+++ b/src/gui/qt/TCPSessionIO.cpp
@@ -30,6 +30,7 @@ TCPSessionIO::TCPSessionIO(const QString &hostname, Q_UINT16 port)
   , mHostname(hostname)
   , mPort(port)
   , mNbConnectTries(0)
+  , mNbConnectTriesTotal(0)
 {
   mReconnectTimer = new QTimer(this);
   QObject::connect(mReconnectTimer, SIGNAL(timeout()), 
@@ -61,15 +62,20 @@ TCPSessionIO::resetConnectionTries()
 void 
 TCPSessionIO::error(int err)
 {
-  mNbConnectTries++;
-  if(mNbConnectTries >= NB_MAX_TRIES) {
-    DebugOutput::instance() << QObject::tr("TCPSessionIO: Connection failed: %1\n")
-      .arg(err);
-    mNbConnectTries = 0;
-    emit disconnected();
-  }
-  else {
-    mReconnectTimer->start(2000, true);
+  if (mNbConnectTriesTotal == 0) {
+    emit firstConnectionFailed();
+    mReconnectTimer->start(4000, true);
+    mNbConnectTriesTotal++;
+  } else {
+    mNbConnectTriesTotal++;
+    mNbConnectTries++;
+    if(mNbConnectTries >= NB_MAX_TRIES) {
+      DebugOutput::instance() << QObject::tr("TCPSessionIO: Connection failed: %1\n").arg(err);
+      mNbConnectTries = 0;
+      emit disconnected();
+    } else {
+     mReconnectTimer->start(2000, true);
+    }
   }
   //mSocket->close();
 }
diff --git a/src/gui/qt/TCPSessionIO.hpp b/src/gui/qt/TCPSessionIO.hpp
index 6ddf34df64..31915d3221 100644
--- a/src/gui/qt/TCPSessionIO.hpp
+++ b/src/gui/qt/TCPSessionIO.hpp
@@ -1,4 +1,4 @@
-/**
+/*
  *  Copyright (C) 2004-2005 Savoir-Faire Linux inc.
  *  Author: Jean-Philippe Barrette-LaPierre
  *             <jean-philippe.barrette-lapierre@savoirfairelinux.com>
@@ -80,7 +80,7 @@ public slots:
 
   void resetConnectionTries();
 
- private slots:
+private slots:
   /**
    * This function is called when we have an error
    * on the socket.
@@ -94,6 +94,7 @@ private:
 
   std::list< QString > mStack;
 
+  unsigned int mNbConnectTriesTotal;
   unsigned int mNbConnectTries;
   QTimer *mReconnectTimer;
 };
diff --git a/src/gui/qt/main.cpp b/src/gui/qt/main.cpp
index 4292889603..6dcaef46f4 100644
--- a/src/gui/qt/main.cpp
+++ b/src/gui/qt/main.cpp
@@ -56,7 +56,6 @@ int main(int argc, char **argv)
   app.setMainWidget(sfl);
 #endif
 
-  app.launch();
   PhoneLineManager::instance().connect();
   //splash->finish(sfl);
   //sfl->show();
-- 
GitLab