implements current account selection

This patch implements the ui and logic of selecting the
current account used in the ring-win32 client

The selector is contained in a new dedicated widget
which displays an image representing the account selected
(for the moment there is no account related avatar so
the picture is the one from the profile) and the alias of the account
through a QComboBox

Finally, some css styling and search bar behaviour are fixed.

Change-Id: I60be8e352e15c7ffdd969f66df7e011f8212e40c
Reviewed-by: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
diff --git a/RingWinClient.pro b/RingWinClient.pro
index 45d3d68..c9bb0f7 100644
--- a/RingWinClient.pro
+++ b/RingWinClient.pro
@@ -72,7 +72,8 @@
     ringbutton.cpp \
     pathpassworddialog.cpp \
     photoboothdialog.cpp \
-    sendcontactrequestwidget.cpp
+    sendcontactrequestwidget.cpp \
+    currentaccountwidget.cpp
 
 HEADERS  += mainwindow.h \
     callwidget.h \
@@ -108,7 +109,8 @@
     ringbutton.h \
     pathpassworddialog.h \
     photoboothdialog.h \
-    sendcontactrequestwidget.h
+    sendcontactrequestwidget.h \
+    currentaccountwidget.h
 
 contains(DEFINES, URI_PROTOCOL) {
  HEADERS += shmclient.h
@@ -132,7 +134,8 @@
     ringbutton.ui \
     pathpassworddialog.ui \
     photoboothdialog.ui \
-    sendcontactrequestwidget.ui
+    sendcontactrequestwidget.ui \
+    currentaccountwidget.ui
 
 win32: LIBS += -lole32 -luuid -lshlwapi
 LIBS += -lqrencode
diff --git a/callwidget.cpp b/callwidget.cpp
index 02d5f95..5f98650 100644
--- a/callwidget.cpp
+++ b/callwidget.cpp
@@ -32,6 +32,7 @@
 #undef interface
 
 #include "audio/settings.h"
+#include "accountmodel.h"
 #include "personmodel.h"
 #include "person.h"
 #include "fallbackpersoncollection.h"
@@ -55,6 +56,7 @@
 #include "settingskey.h"
 
 #include "profilemodel.h"
+#include "profile.h"
 #include "peerprofilecollection.h"
 #include "localprofilecollection.h"
 
@@ -137,8 +139,14 @@
                         ui->smartList->clearSelection();
                 });
 
-        connect(&NameDirectory::instance(), SIGNAL(registeredNameFound(const Account*,NameDirectory::LookupStatus,const QString&,const QString&)),
-                this, SLOT(contactLineEdit_registeredNameFound(const Account*,NameDirectory::LookupStatus,const QString&,const QString&)));
+        connect(&NameDirectory::instance(), SIGNAL(registeredNameFound(Account*,NameDirectory::LookupStatus,const QString&,const QString&)),
+                this, SLOT(contactLineEdit_registeredNameFound(Account*,NameDirectory::LookupStatus,const QString&,const QString&)));
+
+        connect(&AccountModel::instance(), SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),
+                ui->currentAccountWidget, SLOT(update()));
+
+        connect(&ProfileModel::instance(), SIGNAL(ProfileModel::dataChanged(const QModelIndex&,const QModelIndex&, const QVector<int>&)),
+                ui->currentAccountWidget, SLOT(update()));
 
         connect(ui->sendContactRequestWidget, &SendContactRequestWidget::sendCRclicked, [=]{slidePage(ui->messagingPage);});
 
@@ -197,7 +205,8 @@
 }
 
 void
-CallWidget::onIncomingMessage(::Media::TextRecording* t, ContactMethod* cm) {
+CallWidget::onIncomingMessage(::Media::TextRecording* t, ContactMethod* cm)
+{
     Q_UNUSED(cm)
 
     if (!QApplication::focusWidget()) {
@@ -212,7 +221,8 @@
 }
 
 void
-CallWidget::setupSmartListMenu() {
+CallWidget::setupSmartListMenu()
+{
     ui->smartList->setContextMenuPolicy(Qt::CustomContextMenu);
     connect(ui->smartList, &QListView::customContextMenuRequested, [=](const QPoint& pos){
         auto idx = ui->smartList->currentIndex();
@@ -357,11 +367,15 @@
             }
         }
     }
+
     if (ui->ringIdLabel->text().isEmpty()) {
         ui->ringIdLabel->setText(tr("NO RING ACCOUNT FOUND"));
         WizardDialog wizardDialog;
         wizardDialog.exec();
     }
+
+    ui->currentAccountWidget->update();
+
 }
 
 void
@@ -435,7 +449,8 @@
 {
 }
 
-void CallWidget::callStateToView(Call* value)
+void
+CallWidget::callStateToView(Call* value)
 {
     if (value) {
         switch (value->state()) {
@@ -498,6 +513,14 @@
 }
 
 void
+CallWidget::on_smartList_clicked(const QModelIndex& index)
+{
+    RecentModel::instance().selectionModel()->setCurrentIndex(
+                RecentModel::instance().peopleProxy()->mapToSource(index),
+                QItemSelectionModel::ClearAndSelect);
+}
+
+void
 CallWidget::on_smartList_doubleClicked(const QModelIndex& index)
 {
     auto realIndex = RecentModel::instance().peopleProxy()->mapToSource(index);
@@ -739,7 +762,7 @@
 void
 CallWidget::on_ringContactLineEdit_textChanged(const QString& text)
 {
-    RecentModel::instance().peopleProxy()->setFilterWildcard(text);
+    RecentModel::instance().peopleProxy()->setFilterRegExp(QRegExp(text, Qt::CaseInsensitive, QRegExp::FixedString));
 }
 
 void
@@ -763,14 +786,6 @@
 }
 
 void
-CallWidget::on_smartList_clicked(const QModelIndex& index)
-{
-    RecentModel::instance().selectionModel()->setCurrentIndex(
-                RecentModel::instance().peopleProxy()->mapToSource(index),
-                QItemSelectionModel::ClearAndSelect);
-}
-
-void
 CallWidget::on_copyCMButton_clicked()
 {
     auto text = ui->contactMethodComboBox->currentText();
@@ -790,7 +805,7 @@
 }
 
 void
-CallWidget::contactLineEdit_registeredNameFound(const Account* account,NameDirectory::LookupStatus status,
+CallWidget::contactLineEdit_registeredNameFound(Account* account,NameDirectory::LookupStatus status,
                                                 const QString& address,const QString& name)
 {
     URI uri = URI(ui->ringContactLineEdit->text());
@@ -810,7 +825,7 @@
             qDebug() << uri;
             searchContactLineEditEntry(uri);
             auto cm = PhoneDirectoryModel::instance().getNumber(uri);
-            //cm->setAccount(account);
+            cm->setAccount(account);
             break;
         }
         case NameDirectory::LookupStatus::INVALID_NAME:
diff --git a/callwidget.h b/callwidget.h
index 956b207..1ef16fe 100644
--- a/callwidget.h
+++ b/callwidget.h
@@ -91,7 +91,7 @@
     void slotAccountMessageReceived(const QMap<QString,QString> message,ContactMethod* cm,Media::Media::Direction dir);
     void onIncomingMessage(::Media::TextRecording* t, ContactMethod* cm);
     void callChangedSlot();
-    void contactLineEdit_registeredNameFound(const Account* account,NameDirectory::LookupStatus status,const QString& address,const QString& name);
+    void contactLineEdit_registeredNameFound(Account *account, NameDirectory::LookupStatus status, const QString& address, const QString& name);
 
 private:
     Ui::CallWidget* ui;
diff --git a/callwidget.ui b/callwidget.ui
index 74c47f6..c1983ca 100644
--- a/callwidget.ui
+++ b/callwidget.ui
@@ -6,7 +6,7 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>1024</width>
+    <width>1046</width>
     <height>724</height>
    </rect>
   </property>
@@ -112,7 +112,7 @@
           <number>0</number>
          </property>
          <item row="0" column="0">
-          <layout class="QVBoxLayout" name="verticalLayout_2">
+          <layout class="QVBoxLayout" name="verticalLayout_2" stretch="3,1,18">
            <property name="spacing">
             <number>0</number>
            </property>
@@ -120,6 +120,22 @@
             <enum>QLayout::SetDefaultConstraint</enum>
            </property>
            <item>
+            <widget class="CurrentAccountWidget" name="currentAccountWidget" native="true">
+             <property name="sizePolicy">
+              <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
+               <horstretch>0</horstretch>
+               <verstretch>0</verstretch>
+              </sizepolicy>
+             </property>
+             <property name="minimumSize">
+              <size>
+               <width>0</width>
+               <height>100</height>
+              </size>
+             </property>
+            </widget>
+           </item>
+           <item>
             <widget class="QWidget" name="selectBar" native="true">
              <property name="maximumSize">
               <size>
@@ -236,7 +252,7 @@
                 </property>
                 <property name="icon">
                  <iconset resource="ressources.qrc">
-                  <normaloff>:/images/icons/ic_videocam_white.png</normaloff>:/images/icons/ic_videocam_white.png</iconset>
+                  <normaloff>:/images/icons/ic_search_black_18dp_2x.png</normaloff>:/images/icons/ic_search_black_18dp_2x.png</iconset>
                 </property>
                 <property name="iconSize">
                  <size>
@@ -1697,6 +1713,12 @@
    <header>sendcontactrequestwidget.h</header>
    <container>1</container>
   </customwidget>
+  <customwidget>
+   <class>CurrentAccountWidget</class>
+   <extends>QWidget</extends>
+   <header>currentaccountwidget.h</header>
+   <container>1</container>
+  </customwidget>
  </customwidgets>
  <resources>
   <include location="ressources.qrc"/>
diff --git a/currentaccountwidget.cpp b/currentaccountwidget.cpp
new file mode 100644
index 0000000..6cf1bd6
--- /dev/null
+++ b/currentaccountwidget.cpp
@@ -0,0 +1,142 @@
+/***************************************************************************
+ * Copyright (C) 2015-2016 by Savoir-faire Linux                           *
+ * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>          *
+ * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>          *
+ *                                                                         *
+ * This program is free software; you can redistribute it and/or modify    *
+ * it under the terms of the GNU General Public License as published by    *
+ * the Free Software Foundation; either version 3 of the License, or       *
+ * (at your option) any later version.                                     *
+ *                                                                         *
+ * This program is distributed in the hope that it will be useful,         *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
+ * GNU General Public License for more details.                            *
+ *                                                                         *
+ * You should have received a copy of the GNU General Public License       *
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
+ **************************************************************************/
+
+#include "currentaccountwidget.h"
+#include "ui_currentaccountwidget.h"
+#include "globalinstances.h"
+#include "accountmodel.h"
+#include "account.h"
+#include "qstandarditemmodel.h"
+#include "profilemodel.h"
+#include "profile.h"
+#include "person.h"
+#include "utils.h"
+
+
+CurrentAccountWidget::CurrentAccountWidget(QWidget *parent) :
+    QWidget(parent),
+    ui(new Ui::CurrentAccountWidget)
+{
+    ui->setupUi(this);
+    setup();
+}
+
+CurrentAccountWidget::~CurrentAccountWidget()
+{
+    delete ui;
+}
+
+void
+CurrentAccountWidget::setup()
+{
+    ui->accountsStatus->setText("No enabled account: impossible to communicate!");
+    ui->accountsStatus->hide();
+    updateAccounts();
+    if (ui->currentAccountSelector->count() > 0) {
+        ui->currentAccountSelector->setCurrentIndex(0);
+        emit ui->currentAccountSelector->currentIndexChanged(0);
+        qDebug() << "CurrentAccount : setup over";
+    } else {
+        qDebug() << "CurrentAccount : No account available";
+    }
+}
+
+void
+CurrentAccountWidget::update()
+{
+    updateAccounts();
+
+}
+
+void
+CurrentAccountWidget::updateAccounts()
+{
+    auto selector = ui->currentAccountSelector;
+    AccountModel* model = &(AccountModel::instance());
+    QModelIndex qIndex;
+
+    selector->clear();
+
+    for (int idx = 0; idx <=  model->rowCount() - 1; idx++){
+        qIndex = model->index(idx);
+        if (! model->getAccountByModelIndex(qIndex)->isEnabled()) {
+            qDebug() << model->getAccountByModelIndex(qIndex)->alias() << " Account is not enabled";
+        } else {
+            selector->addItem(model->getAccountByModelIndex(qIndex)->alias());
+        }
+    }
+
+    if (selector->count() <= 1){
+        selector->hide();
+        if (selector->count() < 1) {
+            ui->accountsStatus->show();
+            setPhoto();
+        } else {
+            ui->accountsStatus->hide();
+        }
+    } else {
+        selector->show();
+        ui->accountsStatus->hide();
+    }
+}
+
+
+
+void
+CurrentAccountWidget::setPhoto()
+{
+    auto selector = ui->currentAccountSelector;
+    if (selector->count() > 0) {
+        if(&ProfileModel::instance()){
+            if (ProfileModel::instance().selectedProfile()) {
+                if (ProfileModel::instance().selectedProfile()->person()){
+                    QImage img = Utils::getCirclePhoto(ProfileModel::instance().selectedProfile()->person()->photo().value<QImage>(),
+                                                       ui->idDisplayLayout->contentsRect().height());
+                    ui->currentAccountPixmap->setPixmap(QPixmap::fromImage(img));
+                    qDebug() << "CurrentAccount : Photo set";
+                } else {
+                    qDebug() << "CurrentAccount : selected profile has no person";
+                }
+            } else {
+                qDebug() << "CurrentAccount : Profilemodel: no selected profile";
+            }
+        } else {
+            qDebug() << "CurrentAccount : Profilemodel not set";
+        }
+    } else {
+        qDebug() << "CurrentAccount : account not set";
+        ui->currentAccountPixmap->setPixmap(QPixmap());
+    }
+}
+
+void
+CurrentAccountWidget::on_currentAccountSelector_currentIndexChanged(int index)
+{
+    QModelIndex idx = ui->currentAccountSelector->model()->index(index,0);
+    Account* ac = AccountModel::instance().getAccountByModelIndex(idx);
+
+    if (ac) {
+        AccountModel::instance().setUserChosenAccount(ac);
+        setPhoto();
+    } else {
+        qDebug() << "CurrentAccount : account not referenced correctly";
+        //null for now
+    }
+}
+
diff --git a/currentaccountwidget.h b/currentaccountwidget.h
new file mode 100644
index 0000000..8f2e854
--- /dev/null
+++ b/currentaccountwidget.h
@@ -0,0 +1,31 @@
+#ifndef CURRENTACCOUNTWIDGET_H
+#define CURRENTACCOUNTWIDGET_H
+
+#include <QWidget>
+
+namespace Ui {
+class CurrentAccountWidget;
+}
+
+class CurrentAccountWidget : public QWidget
+{
+    Q_OBJECT
+
+public:
+    explicit CurrentAccountWidget(QWidget *parent = 0);
+    ~CurrentAccountWidget();
+
+public slots:
+    void update();
+
+private slots:
+    void on_currentAccountSelector_currentIndexChanged(int index);
+
+private:
+    Ui::CurrentAccountWidget *ui;
+    void setup();
+    void setPhoto();
+    void updateAccounts();
+};
+
+#endif // CURRENTACCOUNTWIDGET_H
diff --git a/currentaccountwidget.ui b/currentaccountwidget.ui
new file mode 100644
index 0000000..fcbe792
--- /dev/null
+++ b/currentaccountwidget.ui
@@ -0,0 +1,141 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>CurrentAccountWidget</class>
+ <widget class="QWidget" name="CurrentAccountWidget">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>294</width>
+    <height>184</height>
+   </rect>
+  </property>
+  <property name="sizePolicy">
+   <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
+    <horstretch>0</horstretch>
+    <verstretch>0</verstretch>
+   </sizepolicy>
+  </property>
+  <property name="minimumSize">
+   <size>
+    <width>0</width>
+    <height>100</height>
+   </size>
+  </property>
+  <property name="windowTitle">
+   <string>Form</string>
+  </property>
+  <property name="autoFillBackground">
+   <bool>false</bool>
+  </property>
+  <property name="styleSheet">
+   <string notr="true"/>
+  </property>
+  <layout class="QHBoxLayout" name="horizontalLayout_2">
+   <property name="spacing">
+    <number>0</number>
+   </property>
+   <property name="leftMargin">
+    <number>0</number>
+   </property>
+   <property name="topMargin">
+    <number>0</number>
+   </property>
+   <property name="rightMargin">
+    <number>0</number>
+   </property>
+   <property name="bottomMargin">
+    <number>0</number>
+   </property>
+   <item>
+    <layout class="QVBoxLayout" name="fullLayout" stretch="4,0,0">
+     <property name="spacing">
+      <number>0</number>
+     </property>
+     <item>
+      <layout class="QHBoxLayout" name="idDisplayLayout" stretch="0,0,0">
+       <property name="leftMargin">
+        <number>10</number>
+       </property>
+       <property name="topMargin">
+        <number>3</number>
+       </property>
+       <property name="rightMargin">
+        <number>10</number>
+       </property>
+       <property name="bottomMargin">
+        <number>3</number>
+       </property>
+       <item>
+        <spacer name="horizontalSpacer_2">
+         <property name="orientation">
+          <enum>Qt::Horizontal</enum>
+         </property>
+         <property name="sizeHint" stdset="0">
+          <size>
+           <width>40</width>
+           <height>20</height>
+          </size>
+         </property>
+        </spacer>
+       </item>
+       <item alignment="Qt::AlignHCenter|Qt::AlignVCenter">
+        <widget class="QLabel" name="currentAccountPixmap">
+         <property name="sizePolicy">
+          <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
+           <horstretch>0</horstretch>
+           <verstretch>0</verstretch>
+          </sizepolicy>
+         </property>
+         <property name="minimumSize">
+          <size>
+           <width>0</width>
+           <height>60</height>
+          </size>
+         </property>
+         <property name="text">
+          <string/>
+         </property>
+        </widget>
+       </item>
+       <item>
+        <spacer name="horizontalSpacer">
+         <property name="orientation">
+          <enum>Qt::Horizontal</enum>
+         </property>
+         <property name="sizeHint" stdset="0">
+          <size>
+           <width>40</width>
+           <height>20</height>
+          </size>
+         </property>
+        </spacer>
+       </item>
+      </layout>
+     </item>
+     <item alignment="Qt::AlignHCenter">
+      <widget class="QLabel" name="accountsStatus">
+       <property name="minimumSize">
+        <size>
+         <width>20</width>
+         <height>10</height>
+        </size>
+       </property>
+       <property name="styleSheet">
+        <string notr="true">text: centered;</string>
+       </property>
+       <property name="text">
+        <string/>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QComboBox" name="currentAccountSelector"/>
+     </item>
+    </layout>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/stylesheet.css b/stylesheet.css
index 0df8c14..edb9aa9 100644
--- a/stylesheet.css
+++ b/stylesheet.css
@@ -195,8 +195,8 @@
     background-color: #515151;
 }
 
-QPushButton#exitSettingsButton:pressed, QPushButton#settingsButton:pressed, QToolButton#imBackButton:pressed,
-QToolButton#sendCRBackButton:pressed, QPushButton#copyCMButton:pressed{
+QPushButton#exitSettingsButton:pressed, QPushButton#settingsButton:pressed, QPushButton#imBackButton:pressed,
+QPushButton#sendCRBackButton:pressed, QPushButton#copyCMButton:pressed{
     background-color: #313131;
 }
 
@@ -509,7 +509,7 @@
 QLineEdit#lrcfg_mailbox, QLineEdit#lrcfg_tlsPassword, QLineEdit#lrcfg_tlsServerName,
 QLineEdit#lrcfg_turnServerRealm, QLineEdit#lrcfg_turnServerUsername, QLineEdit#lrcfg_turnServerPassword,
 QLineEdit#lrcfg_turnServer, QLineEdit#lrcfg_publishedAddress, QLineEdit#lrcfg_registeredName,
-QLineEdit#lrcfg_nameServiceURL, QLineEdit#lrcfg_username{
+QLineEdit#lrcfg_nameServiceURL, QLineEdit#lrcfg_username, QLineEdit#profileNameEdit{
     background: transparent;
     border-radius: 0px;
     border-style: solid;
@@ -533,7 +533,8 @@
 QLineEdit#lrcfg_proxy:focus, QLineEdit#lrcfg_mailbox:focus, QLineEdit#lrcfg_tlsPassword:focus,
 QLineEdit#lrcfg_tlsServerName:focus, QLineEdit#lrcfg_turnServerRealm:focus, QLineEdit#lrcfg_turnServerUsername:focus,
 QLineEdit#lrcfg_turnServerPassword:focus, QLineEdit#lrcfg_turnServer:focus, QLineEdit#lrcfg_publishedAddress:focus,
-QLineEdit#lrcfg_registeredName:focus, QLineEdit#lrcfg_nameServiceURL:focus, QLineEdit#lrcfg_username:focus{
+QLineEdit#lrcfg_registeredName:focus, QLineEdit#lrcfg_nameServiceURL:focus, QLineEdit#lrcfg_username:focus,
+QLineEdit#profileNameEdit:focus{
     border-color: #3AC0D2;
 }