diff options
Diffstat (limited to 'Swift/QtUI/ChatList')
-rw-r--r-- | Swift/QtUI/ChatList/ChatListDelegate.cpp | 211 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListDelegate.h | 50 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListGroupItem.h | 55 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListItem.h | 27 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListMUCItem.cpp | 36 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListMUCItem.h | 44 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListModel.cpp | 200 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListModel.h | 64 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListRecentItem.cpp | 45 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListRecentItem.h | 45 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListWhiteboardItem.cpp | 70 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/ChatListWhiteboardItem.h | 43 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/QtChatListWindow.cpp | 244 | ||||
-rw-r--r-- | Swift/QtUI/ChatList/QtChatListWindow.h | 97 |
14 files changed, 671 insertions, 560 deletions
diff --git a/Swift/QtUI/ChatList/ChatListDelegate.cpp b/Swift/QtUI/ChatList/ChatListDelegate.cpp index 5b03ac5..f818e50 100644 --- a/Swift/QtUI/ChatList/ChatListDelegate.cpp +++ b/Swift/QtUI/ChatList/ChatListDelegate.cpp @@ -1,141 +1,148 @@ /* - * Copyright (c) 2010-2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include <QPen> +#include <Swift/QtUI/ChatList/ChatListDelegate.h> + +#include <QColor> #include <QPainter> +#include <QPen> -#include "Swift/QtUI/ChatList/ChatListDelegate.h" -#include "Swift/QtUI/Roster/GroupItemDelegate.h" -#include "Swift/QtUI/ChatList/ChatListItem.h" -#include "Swift/QtUI/ChatList/ChatListMUCItem.h" -#include "Swift/QtUI/ChatList/ChatListRecentItem.h" -#include "Swift/QtUI/ChatList/ChatListWhiteboardItem.h" -#include "Swift/QtUI/ChatList/ChatListGroupItem.h" +#include <Swift/QtUI/ChatList/ChatListGroupItem.h> +#include <Swift/QtUI/ChatList/ChatListItem.h> +#include <Swift/QtUI/ChatList/ChatListMUCItem.h> +#include <Swift/QtUI/ChatList/ChatListRecentItem.h> +#include <Swift/QtUI/ChatList/ChatListWhiteboardItem.h> +#include <Swift/QtUI/Roster/GroupItemDelegate.h> namespace Swift { +namespace { + const QColor secondLineColor = QColor(160,160,160); +} + ChatListDelegate::ChatListDelegate(bool compact) : compact_(compact) { - groupDelegate_ = new GroupItemDelegate(); + groupDelegate_ = new GroupItemDelegate(); } ChatListDelegate::~ChatListDelegate() { - delete groupDelegate_; + delete groupDelegate_; } void ChatListDelegate::setCompact(bool compact) { - compact_ = compact; + compact_ = compact; + emit sizeHintChanged(QModelIndex()); } QSize ChatListDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index ) const { - ChatListItem* item = static_cast<ChatListItem*>(index.internalPointer()); - if (item && dynamic_cast<ChatListMUCItem*>(item)) { - return mucSizeHint(option, index); - } - else if (item && dynamic_cast<ChatListRecentItem*>(item)) { - return common_.contactSizeHint(option, index, compact_); - } - else if (item && dynamic_cast<ChatListGroupItem*>(item)) { - return groupDelegate_->sizeHint(option, index); - } - else if (item && dynamic_cast<ChatListWhiteboardItem*>(item)) { - return common_.contactSizeHint(option, index, compact_); - } - return QStyledItemDelegate::sizeHint(option, index); + ChatListItem* item = static_cast<ChatListItem*>(index.internalPointer()); + if (item && dynamic_cast<ChatListMUCItem*>(item)) { + return mucSizeHint(option, index); + } + else if (item && dynamic_cast<ChatListRecentItem*>(item)) { + return common_.contactSizeHint(option, index, compact_); + } + else if (item && dynamic_cast<ChatListGroupItem*>(item)) { + return groupDelegate_->sizeHint(option, index); + } + else if (item && dynamic_cast<ChatListWhiteboardItem*>(item)) { + return common_.contactSizeHint(option, index, compact_); + } + return QStyledItemDelegate::sizeHint(option, index); } QSize ChatListDelegate::mucSizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const { - QFontMetrics nameMetrics(common_.nameFont); - QFontMetrics statusMetrics(common_.detailFont); - int sizeByText = 2 * common_.verticalMargin + nameMetrics.height() + statusMetrics.height(); - return QSize(150, sizeByText); + QFontMetrics nameMetrics(common_.nameFont); + QFontMetrics statusMetrics(common_.detailFont); + int sizeByText = 2 * common_.verticalMargin + nameMetrics.height() + statusMetrics.height(); + return QSize(150, sizeByText); } QSize ChatListDelegate::recentSizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { - return mucSizeHint(option, index); + return mucSizeHint(option, index); } void ChatListDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { - ChatListItem* item = static_cast<ChatListItem*>(index.internalPointer()); - if (item && dynamic_cast<ChatListMUCItem*>(item)) { - paintMUC(painter, option, dynamic_cast<ChatListMUCItem*>(item)); - } - else if (item && dynamic_cast<ChatListRecentItem*>(item)) { - paintRecent(painter, option, dynamic_cast<ChatListRecentItem*>(item)); - } - else if (item && dynamic_cast<ChatListGroupItem*>(item)) { - ChatListGroupItem* group = dynamic_cast<ChatListGroupItem*>(item); - groupDelegate_->paint(painter, option, group->data(Qt::DisplayRole).toString(), group->rowCount(), option.state & QStyle::State_Open); - } - else if (item && dynamic_cast<ChatListWhiteboardItem*>(item)) { - paintWhiteboard(painter, option, dynamic_cast<ChatListWhiteboardItem*>(item)); - } - else { - QStyledItemDelegate::paint(painter, option, index); - } + ChatListItem* item = static_cast<ChatListItem*>(index.internalPointer()); + if (item && dynamic_cast<ChatListMUCItem*>(item)) { + paintMUC(painter, option, dynamic_cast<ChatListMUCItem*>(item)); + } + else if (item && dynamic_cast<ChatListRecentItem*>(item)) { + paintRecent(painter, option, dynamic_cast<ChatListRecentItem*>(item)); + } + else if (item && dynamic_cast<ChatListGroupItem*>(item)) { + ChatListGroupItem* group = dynamic_cast<ChatListGroupItem*>(item); + groupDelegate_->paint(painter, option, group->data(Qt::DisplayRole).toString(), group->rowCount(), option.state & QStyle::State_Open); + } + else if (item && dynamic_cast<ChatListWhiteboardItem*>(item)) { + paintWhiteboard(painter, option, dynamic_cast<ChatListWhiteboardItem*>(item)); + } + else { + QStyledItemDelegate::paint(painter, option, index); + } } void ChatListDelegate::paintMUC(QPainter* painter, const QStyleOptionViewItem& option, ChatListMUCItem* item) const { - painter->save(); - QRect fullRegion(option.rect); - if ( option.state & QStyle::State_Selected ) { - painter->fillRect(fullRegion, option.palette.highlight()); - painter->setPen(option.palette.highlightedText().color()); - } else { - QColor nameColor = item->data(Qt::TextColorRole).value<QColor>(); - painter->setPen(QPen(nameColor)); - } - - QFontMetrics nameMetrics(common_.nameFont); - painter->setFont(common_.nameFont); - int extraFontWidth = nameMetrics.width("H"); - int leftOffset = common_.horizontalMargin * 2 + extraFontWidth / 2; - QRect textRegion(fullRegion.adjusted(leftOffset, 0, 0, 0)); - - int nameHeight = nameMetrics.height() + common_.verticalMargin; - QRect nameRegion(textRegion.adjusted(0, common_.verticalMargin, 0, 0)); - - DelegateCommons::drawElidedText(painter, nameRegion, item->data(Qt::DisplayRole).toString()); - - painter->setFont(common_.detailFont); - painter->setPen(QPen(QColor(160,160,160))); - - QRect detailRegion(textRegion.adjusted(0, nameHeight, 0, 0)); - DelegateCommons::drawElidedText(painter, detailRegion, item->data(ChatListMUCItem::DetailTextRole).toString()); - - painter->restore(); + painter->save(); + QRect fullRegion(option.rect); + if ( option.state & QStyle::State_Selected ) { + painter->fillRect(fullRegion, option.palette.highlight()); + painter->setPen(option.palette.highlightedText().color()); + } else { + QColor nameColor = item->data(Qt::TextColorRole).value<QColor>(); + painter->setPen(QPen(nameColor)); + } + + QFontMetrics nameMetrics(common_.nameFont); + painter->setFont(common_.nameFont); + int extraFontWidth = nameMetrics.width("H"); + int leftOffset = common_.horizontalMargin * 2 + extraFontWidth / 2; + QRect textRegion(fullRegion.adjusted(leftOffset, 0, 0, 0)); + + int nameHeight = nameMetrics.height() + common_.verticalMargin; + QRect nameRegion(textRegion.adjusted(0, common_.verticalMargin, 0, 0)); + + DelegateCommons::drawElidedText(painter, nameRegion, item->data(Qt::DisplayRole).toString()); + + painter->setFont(common_.detailFont); + painter->setPen(QPen(secondLineColor)); + + QRect detailRegion(textRegion.adjusted(0, nameHeight, 0, 0)); + DelegateCommons::drawElidedText(painter, detailRegion, item->data(ChatListMUCItem::DetailTextRole).toString()); + + painter->restore(); } void ChatListDelegate::paintRecent(QPainter* painter, const QStyleOptionViewItem& option, ChatListRecentItem* item) const { - QColor nameColor = item->data(Qt::TextColorRole).value<QColor>(); - QString avatarPath; - if (item->data(ChatListRecentItem::AvatarRole).isValid() && !item->data(ChatListRecentItem::AvatarRole).value<QString>().isNull()) { - avatarPath = item->data(ChatListRecentItem::AvatarRole).value<QString>(); - } - QIcon presenceIcon = item->data(ChatListRecentItem::PresenceIconRole).isValid() && !item->data(ChatListRecentItem::PresenceIconRole).value<QIcon>().isNull() - ? item->data(ChatListRecentItem::PresenceIconRole).value<QIcon>() - : QIcon(":/icons/offline.png"); - QString name = item->data(Qt::DisplayRole).toString(); - //qDebug() << "Avatar for " << name << " = " << avatarPath; - QString statusText = item->data(ChatListRecentItem::DetailTextRole).toString(); - common_.paintContact(painter, option, nameColor, avatarPath, presenceIcon, name, statusText, false, item->getChat().unreadCount, compact_); + QColor nameColor = item->data(Qt::TextColorRole).value<QColor>(); + QString avatarPath; + if (item->data(ChatListRecentItem::AvatarRole).isValid() && !item->data(ChatListRecentItem::AvatarRole).value<QString>().isNull()) { + avatarPath = item->data(ChatListRecentItem::AvatarRole).value<QString>(); + } + QIcon presenceIcon = item->data(ChatListRecentItem::PresenceIconRole).isValid() && !item->data(ChatListRecentItem::PresenceIconRole).value<QIcon>().isNull() + ? item->data(ChatListRecentItem::PresenceIconRole).value<QIcon>() + : QIcon(":/icons/offline.png"); + QString name = item->data(Qt::DisplayRole).toString(); + //qDebug() << "Avatar for " << name << " = " << avatarPath; + QString statusText = item->data(ChatListRecentItem::DetailTextRole).toString(); + common_.paintContact(painter, option, nameColor, avatarPath, presenceIcon, name, statusText, false, item->getChat().unreadCount, compact_); } void ChatListDelegate::paintWhiteboard(QPainter* painter, const QStyleOptionViewItem& option, ChatListWhiteboardItem* item) const { - QColor nameColor = item->data(Qt::TextColorRole).value<QColor>(); - QString avatarPath; - if (item->data(ChatListWhiteboardItem::AvatarRole).isValid() && !item->data(ChatListWhiteboardItem::AvatarRole).value<QString>().isNull()) { - avatarPath = item->data(ChatListWhiteboardItem::AvatarRole).value<QString>(); - } - QIcon presenceIcon;/* = item->data(ChatListWhiteboardItem::PresenceIconRole).isValid() && !item->data(ChatListWhiteboardItem::PresenceIconRole).value<QIcon>().isNull() - ? item->data(ChatListWhiteboardItem::PresenceIconRole).value<QIcon>() - : QIcon(":/icons/offline.png");*/ - QString name = item->data(Qt::DisplayRole).toString(); - //qDebug() << "Avatar for " << name << " = " << avatarPath; - QString statusText = item->data(ChatListWhiteboardItem::DetailTextRole).toString(); - common_.paintContact(painter, option, nameColor, avatarPath, presenceIcon, name, statusText, false, item->getChat().unreadCount, compact_); + QColor nameColor = item->data(Qt::TextColorRole).value<QColor>(); + QString avatarPath; + if (item->data(ChatListWhiteboardItem::AvatarRole).isValid() && !item->data(ChatListWhiteboardItem::AvatarRole).value<QString>().isNull()) { + avatarPath = item->data(ChatListWhiteboardItem::AvatarRole).value<QString>(); + } + QIcon presenceIcon;/* = item->data(ChatListWhiteboardItem::PresenceIconRole).isValid() && !item->data(ChatListWhiteboardItem::PresenceIconRole).value<QIcon>().isNull() + ? item->data(ChatListWhiteboardItem::PresenceIconRole).value<QIcon>() + : QIcon(":/icons/offline.png");*/ + QString name = item->data(Qt::DisplayRole).toString(); + //qDebug() << "Avatar for " << name << " = " << avatarPath; + QString statusText = item->data(ChatListWhiteboardItem::DetailTextRole).toString(); + common_.paintContact(painter, option, nameColor, avatarPath, presenceIcon, name, statusText, false, item->getChat().unreadCount, compact_); } diff --git a/Swift/QtUI/ChatList/ChatListDelegate.h b/Swift/QtUI/ChatList/ChatListDelegate.h index 9460c28..44ca947 100644 --- a/Swift/QtUI/ChatList/ChatListDelegate.h +++ b/Swift/QtUI/ChatList/ChatListDelegate.h @@ -1,38 +1,38 @@ /* - * Copyright (c) 2010-2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <QStyledItemDelegate> -#include "Swift/QtUI/Roster/GroupItemDelegate.h" +#include <Swift/QtUI/Roster/GroupItemDelegate.h> namespace Swift { - class ChatListMUCItem; - class ChatListRecentItem; - class ChatListWhiteboardItem; - class ChatListDelegate : public QStyledItemDelegate { - public: - ChatListDelegate(bool compact); - ~ChatListDelegate(); - QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const; - void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; - public slots: - void setCompact(bool compact); - private: - void paintMUC(QPainter* painter, const QStyleOptionViewItem& option, ChatListMUCItem* item) const; - void paintRecent(QPainter* painter, const QStyleOptionViewItem& option, ChatListRecentItem* item) const; - void paintWhiteboard(QPainter* painter, const QStyleOptionViewItem& option, ChatListWhiteboardItem* item) const; - QSize mucSizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const; - QSize recentSizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const; + class ChatListMUCItem; + class ChatListRecentItem; + class ChatListWhiteboardItem; + class ChatListDelegate : public QStyledItemDelegate { + public: + ChatListDelegate(bool compact); + ~ChatListDelegate(); + QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const; + void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; + public slots: + void setCompact(bool compact); + private: + void paintMUC(QPainter* painter, const QStyleOptionViewItem& option, ChatListMUCItem* item) const; + void paintRecent(QPainter* painter, const QStyleOptionViewItem& option, ChatListRecentItem* item) const; + void paintWhiteboard(QPainter* painter, const QStyleOptionViewItem& option, ChatListWhiteboardItem* item) const; + QSize mucSizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const; + QSize recentSizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/ ) const; - bool compact_; - DelegateCommons common_; - GroupItemDelegate* groupDelegate_; - }; + bool compact_; + DelegateCommons common_; + GroupItemDelegate* groupDelegate_; + }; } diff --git a/Swift/QtUI/ChatList/ChatListGroupItem.h b/Swift/QtUI/ChatList/ChatListGroupItem.h index 17defea..a9bb9b1 100644 --- a/Swift/QtUI/ChatList/ChatListGroupItem.h +++ b/Swift/QtUI/ChatList/ChatListGroupItem.h @@ -1,35 +1,42 @@ /* - * Copyright (c) 2010-2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <QList> -#include "Swift/QtUI/ChatList/ChatListItem.h" +#include <Swift/QtUI/ChatList/ChatListItem.h> namespace Swift { - class ChatListGroupItem : public ChatListItem { - public: - ChatListGroupItem(const QString& name, ChatListGroupItem* parent, bool sorted = true) : ChatListItem(parent), name_(name), sorted_(sorted) {} - void addItem(ChatListItem* item) {items_.push_back(item); if (sorted_) {qStableSort(items_.begin(), items_.end(), pointerItemLessThan);}} - void remove(int index) {items_.removeAt(index);} - int rowCount() {return items_.size();} - ChatListItem* item(int i) {return items_[i];} - int row(ChatListItem* item) {return items_.indexOf(item);} - QVariant data(int role) const {return (role == Qt::DisplayRole) ? name_ : QVariant();} - void clear() {items_.clear();} - private: - static bool pointerItemLessThan(const ChatListItem* first, const ChatListItem* second) { - QString myName = first->data(Qt::DisplayRole).toString().toLower(); - QString theirName = second->data(Qt::DisplayRole).toString().toLower(); - return myName < theirName; - } + class ChatListGroupItem : public ChatListItem { + public: + ChatListGroupItem(const QString& name, ChatListGroupItem* parent, bool sorted = true) : ChatListItem(parent), name_(name), sorted_(sorted) {} + virtual ~ChatListGroupItem() {clear();} + void addItem(ChatListItem* item) {items_.push_back(item); if (sorted_) {qStableSort(items_.begin(), items_.end(), pointerItemLessThan);}} + void remove(int index) {items_.removeAt(index);} + int rowCount() {return items_.size();} + ChatListItem* item(int i) {return items_[i];} + int row(ChatListItem* item) {return items_.indexOf(item);} + QVariant data(int role) const {return (role == Qt::DisplayRole) ? name_ : QVariant();} + void clear() { + for (auto item : items_) { + delete item; + } + items_.clear(); + } - QString name_; - QList<ChatListItem*> items_; - bool sorted_; - }; + private: + static bool pointerItemLessThan(const ChatListItem* first, const ChatListItem* second) { + QString myName = first->data(Qt::DisplayRole).toString().toLower(); + QString theirName = second->data(Qt::DisplayRole).toString().toLower(); + return myName < theirName; + } + + QString name_; + QList<ChatListItem*> items_; + bool sorted_; + }; } diff --git a/Swift/QtUI/ChatList/ChatListItem.h b/Swift/QtUI/ChatList/ChatListItem.h index 28c0f9c..c6fd762 100644 --- a/Swift/QtUI/ChatList/ChatListItem.h +++ b/Swift/QtUI/ChatList/ChatListItem.h @@ -1,25 +1,26 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <QVariant> + #include <qdebug.h> namespace Swift { - class ChatListGroupItem; - class ChatListItem { - public: - ChatListItem(ChatListGroupItem* parent) {parent_ = parent;} - virtual ~ChatListItem() {} + class ChatListGroupItem; + class ChatListItem { + public: + ChatListItem(ChatListGroupItem* parent) {parent_ = parent;} + virtual ~ChatListItem() {} - ChatListGroupItem* parent() {return parent_;} - virtual QVariant data(int role) const = 0; + ChatListGroupItem* parent() {return parent_;} + virtual QVariant data(int role) const = 0; - private: - ChatListGroupItem* parent_; - }; + private: + ChatListGroupItem* parent_; + }; } diff --git a/Swift/QtUI/ChatList/ChatListMUCItem.cpp b/Swift/QtUI/ChatList/ChatListMUCItem.cpp index 68f9581..e701ddc 100644 --- a/Swift/QtUI/ChatList/ChatListMUCItem.cpp +++ b/Swift/QtUI/ChatList/ChatListMUCItem.cpp @@ -1,12 +1,14 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/QtUI/ChatList/ChatListMUCItem.h" +#include <Swift/QtUI/ChatList/ChatListMUCItem.h> -#include "Swift/QtUI/QtSwiftUtil.h" +#include <QColor> + +#include <Swift/QtUI/QtSwiftUtil.h> namespace Swift { ChatListMUCItem::ChatListMUCItem(const MUCBookmark& bookmark, ChatListGroupItem* parent) : ChatListItem(parent), bookmark_(bookmark) { @@ -14,21 +16,21 @@ ChatListMUCItem::ChatListMUCItem(const MUCBookmark& bookmark, ChatListGroupItem* } const MUCBookmark& ChatListMUCItem::getBookmark() const { - return bookmark_; + return bookmark_; } QVariant ChatListMUCItem::data(int role) const { - switch (role) { - case Qt::DisplayRole: return P2QSTRING(bookmark_.getName()); - case DetailTextRole: return P2QSTRING(bookmark_.getRoom().toString()); - /*case Qt::TextColorRole: return textColor_; - case Qt::BackgroundColorRole: return backgroundColor_; - case Qt::ToolTipRole: return isContact() ? toolTipString() : QVariant(); - case StatusTextRole: return statusText_; - case AvatarRole: return avatar_; - case PresenceIconRole: return getPresenceIcon();*/ - default: return QVariant(); - } + switch (role) { + case Qt::DisplayRole: return P2QSTRING(bookmark_.getName()); + case DetailTextRole: return P2QSTRING(bookmark_.getRoom().toString()); + case Qt::TextColorRole: return QColor(89,89,89); + /*case Qt::BackgroundColorRole: return backgroundColor_; + case Qt::ToolTipRole: return isContact() ? toolTipString() : QVariant(); + case StatusTextRole: return statusText_; + case AvatarRole: return avatar_; + case PresenceIconRole: return getPresenceIcon();*/ + default: return QVariant(); + } } } diff --git a/Swift/QtUI/ChatList/ChatListMUCItem.h b/Swift/QtUI/ChatList/ChatListMUCItem.h index 046d1d4..c77c284 100644 --- a/Swift/QtUI/ChatList/ChatListMUCItem.h +++ b/Swift/QtUI/ChatList/ChatListMUCItem.h @@ -1,33 +1,33 @@ /* - * Copyright (c) 2010 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <QList> +#include <memory> -#include <boost/shared_ptr.hpp> +#include <QList> -#include "Swiften/MUC/MUCBookmark.h" +#include <Swiften/MUC/MUCBookmark.h> -#include "Swift/QtUI/ChatList/ChatListItem.h" +#include <Swift/QtUI/ChatList/ChatListItem.h> namespace Swift { - class ChatListMUCItem : public ChatListItem { - public: - enum MUCItemRoles { - DetailTextRole = Qt::UserRole/*, - AvatarRole = Qt::UserRole + 1, - PresenceIconRole = Qt::UserRole + 2, - StatusShowTypeRole = Qt::UserRole + 3*/ - }; - ChatListMUCItem(const MUCBookmark& bookmark, ChatListGroupItem* parent); - const MUCBookmark& getBookmark() const; - QVariant data(int role) const; - private: - MUCBookmark bookmark_; - QList<ChatListItem*> items_; - }; + class ChatListMUCItem : public ChatListItem { + public: + enum MUCItemRoles { + DetailTextRole = Qt::UserRole/*, + AvatarRole = Qt::UserRole + 1, + PresenceIconRole = Qt::UserRole + 2, + StatusShowTypeRole = Qt::UserRole + 3*/ + }; + ChatListMUCItem(const MUCBookmark& bookmark, ChatListGroupItem* parent); + const MUCBookmark& getBookmark() const; + QVariant data(int role) const; + private: + MUCBookmark bookmark_; + QList<ChatListItem*> items_; + }; } diff --git a/Swift/QtUI/ChatList/ChatListModel.cpp b/Swift/QtUI/ChatList/ChatListModel.cpp index 7913c61..416b786 100644 --- a/Swift/QtUI/ChatList/ChatListModel.cpp +++ b/Swift/QtUI/ChatList/ChatListModel.cpp @@ -1,125 +1,187 @@ /* - * Copyright (c) 2010-2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/QtUI/ChatList/ChatListModel.h> +#include <QMimeData> +#include <QUrl> + #include <Swift/QtUI/ChatList/ChatListMUCItem.h> #include <Swift/QtUI/ChatList/ChatListRecentItem.h> #include <Swift/QtUI/ChatList/ChatListWhiteboardItem.h> +#include <Swift/QtUI/QtSwiftUtil.h> namespace Swift { -ChatListModel::ChatListModel() : whiteboards_(NULL) { - root_ = new ChatListGroupItem("", NULL, false); - mucBookmarks_ = new ChatListGroupItem(tr("Bookmarked Rooms"), root_); - recents_ = new ChatListGroupItem(tr("Recent Chats"), root_, false); +ChatListModel::ChatListModel() : whiteboards_(nullptr) { + root_ = new ChatListGroupItem("", nullptr, false); + mucBookmarks_ = new ChatListGroupItem(tr("Bookmarked Rooms"), root_); + recents_ = new ChatListGroupItem(tr("Recent Chats"), root_, false); #ifdef SWIFT_EXPERIMENTAL_WB - whiteboards_ = new ChatListGroupItem(tr("Opened Whiteboards"), root_, false); - root_->addItem(whiteboards_); + whiteboards_ = new ChatListGroupItem(tr("Opened Whiteboards"), root_, false); + root_->addItem(whiteboards_); #endif - root_->addItem(recents_); - root_->addItem(mucBookmarks_); + root_->addItem(recents_); + root_->addItem(mucBookmarks_); + + QModelIndex idx = index(0, 0, QModelIndex()); + while (idx.isValid()) { + if (idx.internalPointer() == mucBookmarks_) { + mucBookmarksIndex_ = idx; + } else if (idx.internalPointer() == recents_) { + recentsIndex_ = idx; + } else if (idx.internalPointer() == whiteboards_) { + whiteboardsIndex_ = idx; + } + idx = index(idx.row() + 1, 0, QModelIndex()); + } +} + +Qt::ItemFlags ChatListModel::flags(const QModelIndex& index) const { + Qt::ItemFlags flags = QAbstractItemModel::flags(index); + if (dynamic_cast<ChatListRecentItem*>(getItemForIndex(index))) { + flags |= Qt::ItemIsDragEnabled; + } + return flags; } void ChatListModel::clearBookmarks() { - emit layoutAboutToBeChanged(); - mucBookmarks_->clear(); - emit layoutChanged(); + beginRemoveRows(mucBookmarksIndex_, 0, mucBookmarks_->rowCount()); + mucBookmarks_->clear(); + endRemoveRows(); } void ChatListModel::addMUCBookmark(const Swift::MUCBookmark& bookmark) { - emit layoutAboutToBeChanged(); - mucBookmarks_->addItem(new ChatListMUCItem(bookmark, mucBookmarks_)); - emit layoutChanged(); - //QModelIndex index = createIndex(mucBookmarks_->rowCount() - 1, 0, mucBookmarks_); - //emit dataChanged(index, index); - //emit dataChanged(parent(index), parent(index)); + beginInsertRows(mucBookmarksIndex_, 0, mucBookmarks_->rowCount()); + mucBookmarks_->addItem(new ChatListMUCItem(bookmark, mucBookmarks_)); + endInsertRows(); } void ChatListModel::removeMUCBookmark(const Swift::MUCBookmark& bookmark) { - for (int i = 0; i < mucBookmarks_->rowCount(); i++) { - ChatListMUCItem* item = dynamic_cast<ChatListMUCItem*>(mucBookmarks_->item(i)); - if (item->getBookmark() == bookmark) { - emit layoutAboutToBeChanged(); - mucBookmarks_->remove(i); - emit layoutChanged(); - break; - } - } + for (int i = 0; i < mucBookmarks_->rowCount(); i++) { + ChatListMUCItem* item = dynamic_cast<ChatListMUCItem*>(mucBookmarks_->item(i)); + if (item->getBookmark() == bookmark) { + beginRemoveRows(mucBookmarksIndex_, i, i+1); + mucBookmarks_->remove(i); + endRemoveRows(); + break; + } + } } void ChatListModel::addWhiteboardSession(const ChatListWindow::Chat& chat) { - emit layoutAboutToBeChanged(); - whiteboards_->addItem(new ChatListWhiteboardItem(chat, whiteboards_)); - emit layoutChanged(); + beginInsertRows(whiteboardsIndex_, 0, whiteboards_->rowCount()); + whiteboards_->addItem(new ChatListWhiteboardItem(chat, whiteboards_)); + endInsertRows(); } void ChatListModel::removeWhiteboardSession(const JID& jid) { - for (int i = 0; i < whiteboards_->rowCount(); i++) { - ChatListWhiteboardItem* item = dynamic_cast<ChatListWhiteboardItem*>(whiteboards_->item(i)); - if (item->getChat().jid == jid) { - emit layoutAboutToBeChanged(); - whiteboards_->remove(i); - emit layoutChanged(); - break; - } - } + for (int i = 0; i < whiteboards_->rowCount(); i++) { + ChatListWhiteboardItem* item = dynamic_cast<ChatListWhiteboardItem*>(whiteboards_->item(i)); + if (item->getChat().jid == jid) { + beginRemoveRows(whiteboardsIndex_, i, i+1); + whiteboards_->remove(i); + endRemoveRows(); + break; + } + } } void ChatListModel::setRecents(const std::list<ChatListWindow::Chat>& recents) { - emit layoutAboutToBeChanged(); - recents_->clear(); - foreach (const ChatListWindow::Chat chat, recents) { - recents_->addItem(new ChatListRecentItem(chat, recents_)); + beginRemoveRows(recentsIndex_, 0, recents_->rowCount()); + recents_->clear(); + endRemoveRows(); + beginInsertRows(recentsIndex_, 0, recents.size()); + for (const auto& chat : recents) { + recents_->addItem(new ChatListRecentItem(chat, recents_)); //whiteboards_->addItem(new ChatListRecentItem(chat, whiteboards_)); - } - emit layoutChanged(); + } + endInsertRows(); +} + +QMimeData* ChatListModel::mimeData(const QModelIndexList& indexes) const { + QMimeData* data = QAbstractItemModel::mimeData(indexes); + ChatListRecentItem *item = dynamic_cast<ChatListRecentItem*>(getItemForIndex(indexes.first())); + if (item == nullptr) { + return data; + } + + QByteArray itemData; + QDataStream dataStream(&itemData, QIODevice::WriteOnly); + const ChatListWindow::Chat& chat = item->getChat(); + + QString mimeType = "application/vnd.swift.contact-jid-list"; + if (!chat.impromptuJIDs.size()) { + if (chat.isMUC) { + mimeType = "application/vnd.swift.contact-jid-muc"; + } + dataStream << P2QSTRING(chat.jid.toString()); + } else { + for (const auto& jid : chat.impromptuJIDs) { + dataStream << P2QSTRING(jid.second.toString()); + } + } + + data->setData(mimeType, itemData); + return data; +} + +const ChatListMUCItem* ChatListModel::getChatListMUCItem(const JID& roomJID) const { + const ChatListMUCItem* mucItem = nullptr; + for (int i = 0; i < mucBookmarks_->rowCount(); i++) { + ChatListMUCItem* item = dynamic_cast<ChatListMUCItem*>(mucBookmarks_->item(i)); + if (item->getBookmark().getRoom() == roomJID) { + mucItem = item; + break; + } + } + return mucItem; } int ChatListModel::columnCount(const QModelIndex& /*parent*/) const { - return 1; + return 1; } ChatListItem* ChatListModel::getItemForIndex(const QModelIndex& index) const { - return index.isValid() ? static_cast<ChatListItem*>(index.internalPointer()) : NULL; + return index.isValid() ? static_cast<ChatListItem*>(index.internalPointer()) : nullptr; } QVariant ChatListModel::data(const QModelIndex& index, int role) const { - ChatListItem* item = getItemForIndex(index); - return item ? item->data(role) : QVariant(); + ChatListItem* item = getItemForIndex(index); + return item ? item->data(role) : QVariant(); } QModelIndex ChatListModel::index(int row, int column, const QModelIndex & parent) const { - if (!hasIndex(row, column, parent)) { - return QModelIndex(); - } + if (!hasIndex(row, column, parent)) { + return QModelIndex(); + } - ChatListGroupItem *parentItem = parent.isValid() ? static_cast<ChatListGroupItem*>(parent.internalPointer()) : root_; + ChatListGroupItem *parentItem = parent.isValid() ? static_cast<ChatListGroupItem*>(parent.internalPointer()) : root_; - return row < parentItem->rowCount() ? createIndex(row, column, parentItem->item(row)) : QModelIndex(); + return row < parentItem->rowCount() ? createIndex(row, column, parentItem->item(row)) : QModelIndex(); } QModelIndex ChatListModel::parent(const QModelIndex& index) const { - if (!index.isValid()) { - return QModelIndex(); - } - ChatListGroupItem* parent = static_cast<ChatListGroupItem*>(index.internalPointer())->parent(); - return (parent == root_) ? QModelIndex() : createIndex(parent->parent()->row(parent), 0, parent); + if (!index.isValid()) { + return QModelIndex(); + } + ChatListGroupItem* parent = static_cast<ChatListGroupItem*>(index.internalPointer())->parent(); + return (parent == root_) ? QModelIndex() : createIndex(parent->parent()->row(parent), 0, parent); } int ChatListModel::rowCount(const QModelIndex& parentIndex) const { - ChatListGroupItem* parent = NULL; - if (parentIndex.isValid()) { - parent = dynamic_cast<ChatListGroupItem*>(static_cast<ChatListItem*>(parentIndex.internalPointer())); - } else { - parent = root_; - } - int count = (parent ? parent->rowCount() : 0); - return count; + ChatListGroupItem* parent = nullptr; + if (parentIndex.isValid()) { + parent = dynamic_cast<ChatListGroupItem*>(static_cast<ChatListItem*>(parentIndex.internalPointer())); + } else { + parent = root_; + } + int count = (parent ? parent->rowCount() : 0); + return count; } } diff --git a/Swift/QtUI/ChatList/ChatListModel.h b/Swift/QtUI/ChatList/ChatListModel.h index 04e369a..363b2e6 100644 --- a/Swift/QtUI/ChatList/ChatListModel.h +++ b/Swift/QtUI/ChatList/ChatListModel.h @@ -1,41 +1,53 @@ /* - * Copyright (c) 2010-2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2015 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <QAbstractItemModel> -#include <QList> +#include <QPersistentModelIndex> #include <Swiften/MUC/MUCBookmark.h> + #include <Swift/Controllers/UIInterfaces/ChatListWindow.h> #include <Swift/QtUI/ChatList/ChatListGroupItem.h> namespace Swift { - class ChatListModel : public QAbstractItemModel { - Q_OBJECT - public: - ChatListModel(); - void addMUCBookmark(const MUCBookmark& bookmark); - void removeMUCBookmark(const MUCBookmark& bookmark); - void addWhiteboardSession(const ChatListWindow::Chat& chat); - void removeWhiteboardSession(const JID& jid); - int columnCount(const QModelIndex& parent = QModelIndex()) const; - QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; - QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const; - QModelIndex parent(const QModelIndex& index) const; - int rowCount(const QModelIndex& parent = QModelIndex()) const; - ChatListItem* getItemForIndex(const QModelIndex& index) const; - void clearBookmarks(); - void setRecents(const std::list<ChatListWindow::Chat>& recents); - private: - ChatListGroupItem* mucBookmarks_; - ChatListGroupItem* recents_; - ChatListGroupItem* whiteboards_; - ChatListGroupItem* root_; - }; + class ChatListMUCItem; + + class ChatListModel : public QAbstractItemModel { + Q_OBJECT + public: + ChatListModel(); + Qt::ItemFlags flags(const QModelIndex& index) const; + void addMUCBookmark(const MUCBookmark& bookmark); + void removeMUCBookmark(const MUCBookmark& bookmark); + void addWhiteboardSession(const ChatListWindow::Chat& chat); + void removeWhiteboardSession(const JID& jid); + int columnCount(const QModelIndex& parent = QModelIndex()) const; + QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; + QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const; + QModelIndex parent(const QModelIndex& index) const; + int rowCount(const QModelIndex& parent = QModelIndex()) const; + ChatListItem* getItemForIndex(const QModelIndex& index) const; + void clearBookmarks(); + void setRecents(const std::list<ChatListWindow::Chat>& recents); + QMimeData* mimeData(const QModelIndexList& indexes) const; + + const ChatListMUCItem* getChatListMUCItem(const JID& roomJID) const; + + private: + ChatListGroupItem* mucBookmarks_; + ChatListGroupItem* recents_; + ChatListGroupItem* whiteboards_; + ChatListGroupItem* root_; + + QPersistentModelIndex mucBookmarksIndex_; + QPersistentModelIndex recentsIndex_; + QPersistentModelIndex whiteboardsIndex_; + }; } diff --git a/Swift/QtUI/ChatList/ChatListRecentItem.cpp b/Swift/QtUI/ChatList/ChatListRecentItem.cpp index e9ecec8..9e55e1b 100644 --- a/Swift/QtUI/ChatList/ChatListRecentItem.cpp +++ b/Swift/QtUI/ChatList/ChatListRecentItem.cpp @@ -1,48 +1,41 @@ /* - * Copyright (c) 2011-2013 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <Swift/QtUI/ChatList/ChatListRecentItem.h> -#include <Swift/QtUI/QtSwiftUtil.h> #include <Swiften/Base/Path.h> +#include <Swift/QtUI/QtResourceHelper.h> +#include <Swift/QtUI/QtSwiftUtil.h> + namespace Swift { ChatListRecentItem::ChatListRecentItem(const ChatListWindow::Chat& chat, ChatListGroupItem* parent) : ChatListItem(parent), chat_(chat) { } const ChatListWindow::Chat& ChatListRecentItem::getChat() const { - return chat_; + return chat_; } QVariant ChatListRecentItem::data(int role) const { - switch (role) { - case Qt::DisplayRole: return chat_.impromptuJIDs.empty() ? P2QSTRING(chat_.chatName) : P2QSTRING(chat_.getImpromptuTitle()); - case DetailTextRole: return P2QSTRING(chat_.activity); - /*case Qt::TextColorRole: return textColor_; - case Qt::BackgroundColorRole: return backgroundColor_; - case Qt::ToolTipRole: return isContact() ? toolTipString() : QVariant(); - case StatusTextRole: return statusText_;*/ - case AvatarRole: return QVariant(P2QSTRING(pathToString(chat_.avatarPath))); - case PresenceIconRole: return getPresenceIcon(); - default: return QVariant(); - } + switch (role) { + case Qt::DisplayRole: return P2QSTRING(chat_.getTitle()); + case DetailTextRole: return P2QSTRING(chat_.activity); + case Qt::TextColorRole: return QColor(89,89,89); + /*case Qt::BackgroundColorRole: return backgroundColor_; + case Qt::ToolTipRole: return isContact() ? toolTipString() : QVariant(); + case StatusTextRole: return statusText_;*/ + case AvatarRole: return QVariant(P2QSTRING(pathToString(chat_.avatarPath))); + case PresenceIconRole: return getPresenceIcon(); + default: return QVariant(); + } } QIcon ChatListRecentItem::getPresenceIcon() const { - QString iconString; - switch (chat_.statusType) { - case StatusShow::Online: iconString = "online";break; - case StatusShow::Away: iconString = "away";break; - case StatusShow::XA: iconString = "away";break; - case StatusShow::FFC: iconString = "online";break; - case StatusShow::DND: iconString = "dnd";break; - case StatusShow::None: iconString = "offline";break; - } - return QIcon(":/icons/" + iconString + ".png"); + return QIcon(statusShowTypeToIconPath(chat_.statusType)); } } diff --git a/Swift/QtUI/ChatList/ChatListRecentItem.h b/Swift/QtUI/ChatList/ChatListRecentItem.h index 3f27a68..3c9635b 100644 --- a/Swift/QtUI/ChatList/ChatListRecentItem.h +++ b/Swift/QtUI/ChatList/ChatListRecentItem.h @@ -1,36 +1,37 @@ /* - * Copyright (c) 2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <QList> -#include <QIcon> +#include <memory> -#include <boost/shared_ptr.hpp> +#include <QIcon> +#include <QList> #include <Swiften/MUC/MUCBookmark.h> + #include <Swift/Controllers/UIInterfaces/ChatListWindow.h> #include <Swift/QtUI/ChatList/ChatListItem.h> namespace Swift { - class ChatListRecentItem : public ChatListItem { - public: - enum RecentItemRoles { - DetailTextRole = Qt::UserRole, - AvatarRole = Qt::UserRole + 1, - PresenceIconRole = Qt::UserRole + 2/*, - StatusShowTypeRole = Qt::UserRole + 3, - IdleRole = Qt::UserRole + 4*/ - }; - ChatListRecentItem(const ChatListWindow::Chat& chat, ChatListGroupItem* parent); - const ChatListWindow::Chat& getChat() const; - QVariant data(int role) const; - private: - QIcon getPresenceIcon() const; - ChatListWindow::Chat chat_; - }; + class ChatListRecentItem : public ChatListItem { + public: + enum RecentItemRoles { + DetailTextRole = Qt::UserRole, + AvatarRole = Qt::UserRole + 1, + PresenceIconRole = Qt::UserRole + 2/*, + StatusShowTypeRole = Qt::UserRole + 3, + IdleRole = Qt::UserRole + 4*/ + }; + ChatListRecentItem(const ChatListWindow::Chat& chat, ChatListGroupItem* parent); + const ChatListWindow::Chat& getChat() const; + QVariant data(int role) const; + private: + QIcon getPresenceIcon() const; + ChatListWindow::Chat chat_; + }; } diff --git a/Swift/QtUI/ChatList/ChatListWhiteboardItem.cpp b/Swift/QtUI/ChatList/ChatListWhiteboardItem.cpp index 6791aa5..8a4447e 100644 --- a/Swift/QtUI/ChatList/ChatListWhiteboardItem.cpp +++ b/Swift/QtUI/ChatList/ChatListWhiteboardItem.cpp @@ -5,50 +5,46 @@ */ /* - * Copyright (c) 2013 Remko Tronçon - * Licensed under the GNU General Public License. + * Copyright (c) 2013-2016 Isode Limited. + * All rights reserved. * See the COPYING file for more information. */ #include <Swift/QtUI/ChatList/ChatListWhiteboardItem.h> -#include <Swift/QtUI/QtSwiftUtil.h> +#include <QColor> + #include <Swiften/Base/Path.h> +#include <Swift/QtUI/QtResourceHelper.h> +#include <Swift/QtUI/QtSwiftUtil.h> + namespace Swift { - ChatListWhiteboardItem::ChatListWhiteboardItem(const ChatListWindow::Chat& chat, ChatListGroupItem* parent) : ChatListItem(parent), chat_(chat) { - - } - - const ChatListWindow::Chat& ChatListWhiteboardItem::getChat() const { - return chat_; - } - - QVariant ChatListWhiteboardItem::data(int role) const { - switch (role) { - case Qt::DisplayRole: return P2QSTRING(chat_.chatName); - case DetailTextRole: return P2QSTRING(chat_.activity); - /*case Qt::TextColorRole: return textColor_; - case Qt::BackgroundColorRole: return backgroundColor_; - case Qt::ToolTipRole: return isContact() ? toolTipString() : QVariant(); - case StatusTextRole: return statusText_;*/ - case AvatarRole: return QVariant(P2QSTRING(pathToString(chat_.avatarPath))); - case PresenceIconRole: return getPresenceIcon(); - default: return QVariant(); - } - } - - QIcon ChatListWhiteboardItem::getPresenceIcon() const { - QString iconString; - switch (chat_.statusType) { - case StatusShow::Online: iconString = "online";break; - case StatusShow::Away: iconString = "away";break; - case StatusShow::XA: iconString = "away";break; - case StatusShow::FFC: iconString = "online";break; - case StatusShow::DND: iconString = "dnd";break; - case StatusShow::None: iconString = "offline";break; - } - return QIcon(":/icons/" + iconString + ".png"); - } + ChatListWhiteboardItem::ChatListWhiteboardItem(const ChatListWindow::Chat& chat, ChatListGroupItem* parent) : ChatListItem(parent), chat_(chat) { + + } + + const ChatListWindow::Chat& ChatListWhiteboardItem::getChat() const { + return chat_; + } + + QVariant ChatListWhiteboardItem::data(int role) const { + switch (role) { + case Qt::DisplayRole: return P2QSTRING(chat_.chatName); + case DetailTextRole: return P2QSTRING(chat_.activity); + case Qt::TextColorRole: return QColor(89,89,89); + /*case Qt::TextColorRole: return textColor_; + case Qt::BackgroundColorRole: return backgroundColor_; + case Qt::ToolTipRole: return isContact() ? toolTipString() : QVariant(); + case StatusTextRole: return statusText_;*/ + case AvatarRole: return QVariant(P2QSTRING(pathToString(chat_.avatarPath))); + case PresenceIconRole: return getPresenceIcon(); + default: return QVariant(); + } + } + + QIcon ChatListWhiteboardItem::getPresenceIcon() const { + return QIcon(statusShowTypeToIconPath(chat_.statusType)); + } } diff --git a/Swift/QtUI/ChatList/ChatListWhiteboardItem.h b/Swift/QtUI/ChatList/ChatListWhiteboardItem.h index 2dc6255..6dbc5f6 100644 --- a/Swift/QtUI/ChatList/ChatListWhiteboardItem.h +++ b/Swift/QtUI/ChatList/ChatListWhiteboardItem.h @@ -1,35 +1,36 @@ /* - * Copyright (c) 2012 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <QList> -#include <QIcon> +#include <memory> -#include <boost/shared_ptr.hpp> +#include <QIcon> +#include <QList> #include <Swiften/MUC/MUCBookmark.h> + #include <Swift/Controllers/UIInterfaces/ChatListWindow.h> #include <Swift/QtUI/ChatList/ChatListItem.h> namespace Swift { - class ChatListWhiteboardItem : public ChatListItem { - public: - enum RecentItemRoles { - DetailTextRole = Qt::UserRole, - AvatarRole = Qt::UserRole + 1, - PresenceIconRole = Qt::UserRole + 2/*, - StatusShowTypeRole = Qt::UserRole + 3*/ - }; - ChatListWhiteboardItem(const ChatListWindow::Chat& chat, ChatListGroupItem* parent); - const ChatListWindow::Chat& getChat() const; - QVariant data(int role) const; - private: - QIcon getPresenceIcon() const; - ChatListWindow::Chat chat_; - }; + class ChatListWhiteboardItem : public ChatListItem { + public: + enum RecentItemRoles { + DetailTextRole = Qt::UserRole, + AvatarRole = Qt::UserRole + 1, + PresenceIconRole = Qt::UserRole + 2/*, + StatusShowTypeRole = Qt::UserRole + 3*/ + }; + ChatListWhiteboardItem(const ChatListWindow::Chat& chat, ChatListGroupItem* parent); + const ChatListWindow::Chat& getChat() const; + QVariant data(int role) const; + private: + QIcon getPresenceIcon() const; + ChatListWindow::Chat chat_; + }; } diff --git a/Swift/QtUI/ChatList/QtChatListWindow.cpp b/Swift/QtUI/ChatList/QtChatListWindow.cpp index 4d1f19b..2fd05c4 100644 --- a/Swift/QtUI/ChatList/QtChatListWindow.cpp +++ b/Swift/QtUI/ChatList/QtChatListWindow.cpp @@ -1,15 +1,25 @@ /* - * Copyright (c) 2010-2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include "Swift/QtUI/ChatList/QtChatListWindow.h" +#include <Swift/QtUI/ChatList/QtChatListWindow.h> #include <boost/bind.hpp> -#include <QMenu> #include <QContextMenuEvent> +#include <QMenu> +#include <QMimeData> +#include <QUrl> + +#include <Swift/Controllers/Settings/SettingsProvider.h> +#include <Swift/Controllers/UIEvents/AddMUCBookmarkUIEvent.h> +#include <Swift/Controllers/UIEvents/EditMUCBookmarkUIEvent.h> +#include <Swift/Controllers/UIEvents/JoinMUCUIEvent.h> +#include <Swift/Controllers/UIEvents/RemoveMUCBookmarkUIEvent.h> +#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h> +#include <Swift/Controllers/UIEvents/ShowWhiteboardUIEvent.h> #include <Swift/QtUI/ChatList/ChatListMUCItem.h> #include <Swift/QtUI/ChatList/ChatListRecentItem.h> @@ -17,166 +27,180 @@ #include <Swift/QtUI/QtAddBookmarkWindow.h> #include <Swift/QtUI/QtEditBookmarkWindow.h> #include <Swift/QtUI/QtUISettingConstants.h> -#include <Swift/Controllers/UIEvents/JoinMUCUIEvent.h> -#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h> -#include <Swift/Controllers/UIEvents/AddMUCBookmarkUIEvent.h> -#include <Swift/Controllers/UIEvents/RemoveMUCBookmarkUIEvent.h> -#include <Swift/Controllers/UIEvents/EditMUCBookmarkUIEvent.h> -#include <Swift/Controllers/UIEvents/ShowWhiteboardUIEvent.h> -#include <Swift/Controllers/Settings/SettingsProvider.h> - namespace Swift { -QtChatListWindow::QtChatListWindow(UIEventStream *uiEventStream, SettingsProvider* settings, QWidget* parent) : QTreeView(parent) { - eventStream_ = uiEventStream; - settings_ = settings; - bookmarksEnabled_ = false; - model_ = new ChatListModel(); - setModel(model_); - delegate_ = new ChatListDelegate(settings_->getSetting(QtUISettingConstants::COMPACT_ROSTER)); - setItemDelegate(delegate_); - setHeaderHidden(true); +QtChatListWindow::QtChatListWindow(UIEventStream *uiEventStream, SettingsProvider* settings, QWidget* parent) : QTreeView(parent), isOnline_(false) { + eventStream_ = uiEventStream; + settings_ = settings; + bookmarksEnabled_ = false; + model_ = new ChatListModel(); + setModel(model_); + delegate_ = new ChatListDelegate(settings_->getSetting(QtUISettingConstants::COMPACT_ROSTER)); + setItemDelegate(delegate_); + setHeaderHidden(true); #ifdef SWIFT_PLATFORM_MACOSX - setAlternatingRowColors(true); + setAlternatingRowColors(true); #endif - expandAll(); - setAnimated(true); - setIndentation(0); - setRootIsDecorated(true); - setupContextMenus(); - connect(this, SIGNAL(activated(const QModelIndex&)), this, SLOT(handleItemActivated(const QModelIndex&))); - connect(this, SIGNAL(clicked(const QModelIndex&)), this, SLOT(handleClicked(const QModelIndex&))); + expandAll(); + setAnimated(true); + setIndentation(0); + setDragEnabled(true); + setRootIsDecorated(true); + setupContextMenus(); + connect(this, SIGNAL(activated(const QModelIndex&)), this, SLOT(handleItemActivated(const QModelIndex&))); + connect(this, SIGNAL(clicked(const QModelIndex&)), this, SLOT(handleClicked(const QModelIndex&))); - settings_->onSettingChanged.connect(boost::bind(&QtChatListWindow::handleSettingChanged, this, _1)); + settings_->onSettingChanged.connect(boost::bind(&QtChatListWindow::handleSettingChanged, this, _1)); } QtChatListWindow::~QtChatListWindow() { - settings_->onSettingChanged.disconnect(boost::bind(&QtChatListWindow::handleSettingChanged, this, _1)); - delete model_; - delete delegate_; - delete mucMenu_; - delete emptyMenu_; + settings_->onSettingChanged.disconnect(boost::bind(&QtChatListWindow::handleSettingChanged, this, _1)); + delete model_; + delete delegate_; + delete mucMenu_; + delete emptyMenu_; } void QtChatListWindow::handleSettingChanged(const std::string& setting) { - if (setting == QtUISettingConstants::COMPACT_ROSTER.getKey()) { - delegate_->setCompact(settings_->getSetting(QtUISettingConstants::COMPACT_ROSTER)); - repaint(); - } + if (setting == QtUISettingConstants::COMPACT_ROSTER.getKey()) { + delegate_->setCompact(settings_->getSetting(QtUISettingConstants::COMPACT_ROSTER)); + repaint(); + } +} + +void QtChatListWindow::handleClearRecentsRequested() { + onClearRecentsRequested(); } void QtChatListWindow::setBookmarksEnabled(bool enabled) { - bookmarksEnabled_ = enabled; + bookmarksEnabled_ = enabled; } void QtChatListWindow::handleClicked(const QModelIndex& index) { - ChatListGroupItem* item = dynamic_cast<ChatListGroupItem*>(static_cast<ChatListItem*>(index.internalPointer())); - if (item) { - setExpanded(index, !isExpanded(index)); - } + ChatListGroupItem* item = dynamic_cast<ChatListGroupItem*>(static_cast<ChatListItem*>(index.internalPointer())); + if (item) { + setExpanded(index, !isExpanded(index)); + } } void QtChatListWindow::setupContextMenus() { - mucMenu_ = new QMenu(); - mucMenu_->addAction(tr("Add New Bookmark"), this, SLOT(handleAddBookmark())); - mucMenu_->addAction(tr("Edit Bookmark"), this, SLOT(handleEditBookmark())); - mucMenu_->addAction(tr("Remove Bookmark"), this, SLOT(handleRemoveBookmark())); - emptyMenu_ = new QMenu(); - emptyMenu_->addAction(tr("Add New Bookmark"), this, SLOT(handleAddBookmark())); - + mucMenu_ = new QMenu(); + onlineOnlyActions_ << mucMenu_->addAction(tr("Edit Bookmark"), this, SLOT(handleEditBookmark())); + onlineOnlyActions_ << mucMenu_->addAction(tr("Remove Bookmark"), this, SLOT(handleRemoveBookmark())); + emptyMenu_ = new QMenu(); } void QtChatListWindow::handleItemActivated(const QModelIndex& index) { - ChatListItem* item = model_->getItemForIndex(index); - if (ChatListMUCItem* mucItem = dynamic_cast<ChatListMUCItem*>(item)) { - if (bookmarksEnabled_) { - onMUCBookmarkActivated(mucItem->getBookmark()); - } - } - else if (ChatListRecentItem* recentItem = dynamic_cast<ChatListRecentItem*>(item)) { - if (!recentItem->getChat().isMUC || bookmarksEnabled_) { - onRecentActivated(recentItem->getChat()); - } - } - else if (ChatListWhiteboardItem* whiteboardItem = dynamic_cast<ChatListWhiteboardItem*>(item)) { - if (!whiteboardItem->getChat().isMUC || bookmarksEnabled_) { - eventStream_->send(boost::make_shared<ShowWhiteboardUIEvent>(whiteboardItem->getChat().jid)); - } - } + ChatListItem* item = model_->getItemForIndex(index); + if (ChatListMUCItem* mucItem = dynamic_cast<ChatListMUCItem*>(item)) { + onMUCBookmarkActivated(mucItem->getBookmark()); + } + else if (ChatListRecentItem* recentItem = dynamic_cast<ChatListRecentItem*>(item)) { + onRecentActivated(recentItem->getChat()); + } + else if (ChatListWhiteboardItem* whiteboardItem = dynamic_cast<ChatListWhiteboardItem*>(item)) { + if (!whiteboardItem->getChat().isMUC || bookmarksEnabled_) { + eventStream_->send(std::make_shared<ShowWhiteboardUIEvent>(whiteboardItem->getChat().jid)); + } + } } void QtChatListWindow::clearBookmarks() { - model_->clearBookmarks(); + model_->clearBookmarks(); } void QtChatListWindow::addMUCBookmark(const MUCBookmark& bookmark) { - model_->addMUCBookmark(bookmark); + model_->addMUCBookmark(bookmark); } void QtChatListWindow::removeMUCBookmark(const MUCBookmark& bookmark) { - model_->removeMUCBookmark(bookmark); + model_->removeMUCBookmark(bookmark); } void QtChatListWindow::addWhiteboardSession(const ChatListWindow::Chat& chat) { - model_->addWhiteboardSession(chat); + model_->addWhiteboardSession(chat); } void QtChatListWindow::removeWhiteboardSession(const JID& jid) { - model_->removeWhiteboardSession(jid); + model_->removeWhiteboardSession(jid); } void QtChatListWindow::setRecents(const std::list<ChatListWindow::Chat>& recents) { - model_->setRecents(recents); + model_->setRecents(recents); } -void QtChatListWindow::setUnreadCount(int unread) { - emit onCountUpdated(unread); +void QtChatListWindow::setUnreadCount(size_t unread) { + emit onCountUpdated(unread); } -void QtChatListWindow::handleRemoveBookmark() { - ChatListMUCItem* mucItem = dynamic_cast<ChatListMUCItem*>(contextMenuItem_); - if (!mucItem) return; - eventStream_->send(boost::shared_ptr<UIEvent>(new RemoveMUCBookmarkUIEvent(mucItem->getBookmark()))); +void QtChatListWindow::setOnline(bool isOnline) { + isOnline_ = isOnline; } -void QtChatListWindow::handleAddBookmark() { - (new QtAddBookmarkWindow(eventStream_))->show(); +void QtChatListWindow::handleRemoveBookmark() { + const ChatListMUCItem* mucItem = dynamic_cast<const ChatListMUCItem*>(contextMenuItem_); + if (!mucItem) return; + eventStream_->send(std::make_shared<RemoveMUCBookmarkUIEvent>(mucItem->getBookmark())); } - void QtChatListWindow::handleEditBookmark() { - ChatListMUCItem* mucItem = dynamic_cast<ChatListMUCItem*>(contextMenuItem_); - if (!mucItem) return; - QtEditBookmarkWindow* window = new QtEditBookmarkWindow(eventStream_, mucItem->getBookmark()); - window->show(); + const ChatListMUCItem* mucItem = dynamic_cast<const ChatListMUCItem*>(contextMenuItem_); + if (!mucItem) return; + QtEditBookmarkWindow* window = new QtEditBookmarkWindow(eventStream_, mucItem->getBookmark()); + window->show(); } +void QtChatListWindow::dragEnterEvent(QDragEnterEvent *event) { + if (event->mimeData()->hasUrls() && event->mimeData()->urls().size() == 1) { + event->acceptProposedAction(); + } +} void QtChatListWindow::contextMenuEvent(QContextMenuEvent* event) { - QModelIndex index = indexAt(event->pos()); - ChatListItem* baseItem = index.isValid() ? static_cast<ChatListItem*>(index.internalPointer()) : NULL; - contextMenuItem_ = baseItem; - if (!baseItem) { - emptyMenu_->exec(QCursor::pos()); - return; - } - ChatListMUCItem* mucItem = dynamic_cast<ChatListMUCItem*>(baseItem); - if (mucItem) { - if (!bookmarksEnabled_) { - return; - } - mucMenu_->exec(QCursor::pos()); - } - else { - QMenu menu; - QAction* clearRecents = menu.addAction(tr("Clear recents")); - menu.addAction(clearRecents); - QAction* result = menu.exec(event->globalPos()); - if (result == clearRecents) { - onClearRecentsRequested(); - } - } + QModelIndex index = indexAt(event->pos()); + ChatListItem* baseItem = index.isValid() ? static_cast<ChatListItem*>(index.internalPointer()) : nullptr; + contextMenuItem_ = baseItem; + + for (auto action : onlineOnlyActions_) { + action->setEnabled(isOnline_); + } + + if (!baseItem) { + emptyMenu_->exec(QCursor::pos()); + return; + } + + ChatListMUCItem* mucItem = dynamic_cast<ChatListMUCItem*>(baseItem); + if (mucItem) { + if (!bookmarksEnabled_) { + return; + } + mucMenu_->exec(QCursor::pos()); + return; + } + + ChatListRecentItem* recentItem = dynamic_cast<ChatListRecentItem*>(baseItem); + if (recentItem) { + const ChatListWindow::Chat& chat = recentItem->getChat(); + if (chat.isMUC) { + QMenu mucRecentsMenu; + QAction* bookmarkAction = nullptr; + const ChatListMUCItem* mucItem = model_->getChatListMUCItem(chat.jid); + if (mucItem) { + contextMenuItem_ = mucItem; + bookmarkAction = mucRecentsMenu.addAction(tr("Edit Bookmark"), this, SLOT(handleEditBookmark())); + bookmarkAction->setEnabled(isOnline_); + } + mucRecentsMenu.addAction(tr("Clear recents"), this, SLOT(handleClearRecentsRequested())); + mucRecentsMenu.exec(QCursor::pos()); + return; + } + } + + QMenu menu; + menu.addAction(tr("Clear recents"), this, SLOT(handleClearRecentsRequested())); + menu.exec(event->globalPos()); } } diff --git a/Swift/QtUI/ChatList/QtChatListWindow.h b/Swift/QtUI/ChatList/QtChatListWindow.h index ef4ce0f..3322001 100644 --- a/Swift/QtUI/ChatList/QtChatListWindow.h +++ b/Swift/QtUI/ChatList/QtChatListWindow.h @@ -1,57 +1,62 @@ /* - * Copyright (c) 2010-2011 Kevin Smith - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once #include <QTreeView> -#include "Swift/Controllers/UIInterfaces/ChatListWindow.h" -#include "Swift/Controllers/UIEvents/UIEventStream.h" -#include "Swift/QtUI/ChatList/ChatListModel.h" -#include "Swift/QtUI/ChatList/ChatListDelegate.h" +#include <Swift/Controllers/UIEvents/UIEventStream.h> +#include <Swift/Controllers/UIInterfaces/ChatListWindow.h> + +#include <Swift/QtUI/ChatList/ChatListDelegate.h> +#include <Swift/QtUI/ChatList/ChatListModel.h> namespace Swift { - class SettingsProvider; - class QtChatListWindow : public QTreeView, public ChatListWindow { - Q_OBJECT - public: - QtChatListWindow(UIEventStream *uiEventStream, SettingsProvider* settings, QWidget* parent = NULL); - virtual ~QtChatListWindow(); - void addMUCBookmark(const MUCBookmark& bookmark); - void removeMUCBookmark(const MUCBookmark& bookmark); - void addWhiteboardSession(const ChatListWindow::Chat& chat); - void removeWhiteboardSession(const JID& jid); - void setBookmarksEnabled(bool enabled); - void setRecents(const std::list<ChatListWindow::Chat>& recents); - void setUnreadCount(int unread); - void clearBookmarks(); - - signals: - void onCountUpdated(int count); - private slots: - void handleItemActivated(const QModelIndex&); - void handleAddBookmark(); - void handleEditBookmark(); - void handleRemoveBookmark(); - void handleClicked(const QModelIndex& index); - void handleSettingChanged(const std::string& setting); - - protected: - void contextMenuEvent(QContextMenuEvent* event); - - private: - void setupContextMenus(); - bool bookmarksEnabled_; - UIEventStream* eventStream_; - ChatListModel* model_; - ChatListDelegate* delegate_; - QMenu* mucMenu_; - QMenu* emptyMenu_; - ChatListItem* contextMenuItem_; - SettingsProvider* settings_; - }; + class SettingsProvider; + class QtChatListWindow : public QTreeView, public ChatListWindow { + Q_OBJECT + public: + QtChatListWindow(UIEventStream *uiEventStream, SettingsProvider* settings, QWidget* parent = nullptr); + virtual ~QtChatListWindow(); + void addMUCBookmark(const MUCBookmark& bookmark); + void removeMUCBookmark(const MUCBookmark& bookmark); + void addWhiteboardSession(const ChatListWindow::Chat& chat); + void removeWhiteboardSession(const JID& jid); + void setBookmarksEnabled(bool enabled); + void setRecents(const std::list<ChatListWindow::Chat>& recents); + void setUnreadCount(size_t unread); + void clearBookmarks(); + virtual void setOnline(bool isOnline); + + signals: + void onCountUpdated(size_t count); + private slots: + void handleItemActivated(const QModelIndex&); + void handleEditBookmark(); + void handleRemoveBookmark(); + void handleClicked(const QModelIndex& index); + void handleSettingChanged(const std::string& setting); + void handleClearRecentsRequested(); + + protected: + void dragEnterEvent(QDragEnterEvent* event); + void contextMenuEvent(QContextMenuEvent* event); + + private: + void setupContextMenus(); + bool bookmarksEnabled_; + UIEventStream* eventStream_; + ChatListModel* model_; + ChatListDelegate* delegate_; + QMenu* mucMenu_; + QMenu* emptyMenu_; + const ChatListItem* contextMenuItem_; + SettingsProvider* settings_; + QList<QAction*> onlineOnlyActions_; + bool isOnline_; + }; } |