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

List:       kde-commits
Subject:    =?utf-8?q?=5Btelepathy-chat-handler=5D_/=3A_Avoid_duplicate_tabs?=
From:       David Edmundson <kde () davidedmundson ! co ! uk>
Date:       2011-03-09 22:45:02
Message-ID: 20110309224502.173FEA60B9 () git ! kde ! org
[Download RAW message or body]

Git commit 900fc0843f539bf86e74319ba2c2e89c53a4b4d2 by David Edmundson. on behalf of \
Francesco Nwokeka Committed on 09/03/2011 at 23:41.
Pushed by davidedmundson into branch 'master'.

Avoid duplicate tabs with the same name.
Added a getter method for the channel in the library.
On a new incoming channel, check open tabs for anything already using the same \
channel before creating a new tab.

M  +33   -9    app/chat-window.cpp     
M  +6    -1    app/chat-window.h     
M  +2    -3    app/telepathy-chat-ui.cpp     
M  +4    -0    lib/chat-widget.cpp     
M  +3    -0    lib/chat-widget.h     

http://commits.kde.org/telepathy-chat-handler/900fc0843f539bf86e74319ba2c2e89c53a4b4d2


diff --git a/app/chat-window.cpp b/app/chat-window.cpp
index 66f7c03..3ebe1c0 100644
--- a/app/chat-window.cpp
+++ b/app/chat-window.cpp
@@ -33,6 +33,8 @@
 #include <KSettings/Dialog>
 #include <KNotifyConfigWidget>
 
+#include <TelepathyQt4/TextChannel>
+
 ChatWindow::ChatWindow()
 {
     //setup actions
@@ -61,18 +63,40 @@ ChatWindow::~ChatWindow()
 {
 }
 
-void ChatWindow::addTab(ChatWidget* chatWidget)
+void ChatWindow::startChat(Tp::TextChannelPtr incomingTextChannel)
 {
-    connect(chatWidget, SIGNAL(titleChanged(QString)), this, \
                SLOT(updateTabText(QString)));
-    connect(chatWidget, SIGNAL(iconChanged(KIcon)), this, \
                SLOT(updateTabIcon(KIcon)));
-    connect(chatWidget, SIGNAL(userTypingChanged(bool)), this, \
SLOT(onUserTypingChanged(bool))); +    bool duplicateTab = false;
+
+    // check that the tab requested isn't already open
+    for(int index = 0; index < m_tabWidget->count() && !duplicateTab; index++) {
+
+        // get chatWidget object
+        ChatWidget *auxChatWidget = \
qobject_cast<ChatWidget*>(m_tabWidget->widget(index)); +
+        // this should never happen
+        if(!auxChatWidget)
+            return;
+
+        if(auxChatWidget->textChannel() == incomingTextChannel) {   // got duplicate \
tab +            duplicateTab = true;
+            m_tabWidget->setCurrentIndex(index);                    // set focus on \
selected tab +        }
+    }
+
+    // got new chat, create it
+    if(!duplicateTab) {
+        ChatWidget *chatWidget = new ChatWidget(incomingTextChannel, m_tabWidget);
+        connect(chatWidget, SIGNAL(titleChanged(QString)), this, \
SLOT(updateTabText(QString))); +        connect(chatWidget, \
SIGNAL(iconChanged(KIcon)), this, SLOT(updateTabIcon(KIcon))); +        \
connect(chatWidget, SIGNAL(userTypingChanged(bool)), this, \
SLOT(onUserTypingChanged(bool)));  
-    m_tabWidget->addTab(chatWidget, chatWidget->icon(), chatWidget->title());
-    m_tabWidget->setCurrentWidget(chatWidget);
+        m_tabWidget->addTab(chatWidget, chatWidget->icon(), chatWidget->title());
+        m_tabWidget->setCurrentWidget(chatWidget);
 
-    if(m_tabWidget->isTabBarHidden()) {
-        if(m_tabWidget->count() > 1) {
-            m_tabWidget->setTabBarHidden(false);
+        if(m_tabWidget->isTabBarHidden()) {
+            if(m_tabWidget->count() > 1) {
+                m_tabWidget->setTabBarHidden(false);
+            }
         }
     }
 
diff --git a/app/chat-window.h b/app/chat-window.h
index b023241..e921d12 100644
--- a/app/chat-window.h
+++ b/app/chat-window.h
@@ -35,7 +35,12 @@ public:
     ChatWindow();
     virtual ~ChatWindow();
 
-    void addTab(ChatWidget *chatWidget);
+    /**
+     * starts a new chat with the textChannelPtr given only if the
+     * chat doesn't already exist
+     * @param incomingTextChannel new text channel
+     */
+    void startChat(Tp::TextChannelPtr incomingTextChannel);
 
 public slots:
     void removeTab(QWidget *chatWidget);
diff --git a/app/telepathy-chat-ui.cpp b/app/telepathy-chat-ui.cpp
index 776c148..0845892 100644
--- a/app/telepathy-chat-ui.cpp
+++ b/app/telepathy-chat-ui.cpp
@@ -70,9 +70,8 @@ void TelepathyChatUi::handleChannels(const \
Tp::MethodInvocationContextPtr<> & co  }
     Q_ASSERT(textChannel);
 
-    ChatWidget* newChatWidget = new ChatWidget(textChannel, m_chatWindow);
-
-    m_chatWindow->addTab(newChatWidget);
+    // create new chat
+    m_chatWindow->startChat(textChannel);
 
     m_chatWindow->show();
 
diff --git a/lib/chat-widget.cpp b/lib/chat-widget.cpp
index 5a8bab3..d0b0720 100644
--- a/lib/chat-widget.cpp
+++ b/lib/chat-widget.cpp
@@ -221,6 +221,10 @@ void ChatWidget::resizeEvent(QResizeEvent *e)
     QWidget::resizeEvent(e);
 }
 
+Tp::TextChannelPtr ChatWidget::textChannel() const
+{
+    return d->channel;
+}
 
 QString ChatWidget::title() const
 {
diff --git a/lib/chat-widget.h b/lib/chat-widget.h
index 95d4eb6..ca24bc7 100644
--- a/lib/chat-widget.h
+++ b/lib/chat-widget.h
@@ -38,6 +38,9 @@ public:
     explicit ChatWidget(const Tp::TextChannelPtr & channel, QWidget *parent = 0);
     virtual ~ChatWidget();
 
+    /** Returns the text channel pointer of the chatWidget */
+    Tp::TextChannelPtr textChannel() const;
+
     /** Returns the name of this chat window*/
     QString title() const;
 


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

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