/* * Copyright (c) 2012 Catalin Badea * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Swift { QtHistoryWindow::QtHistoryWindow(SettingsProvider* settings, UIEventStream* eventStream) { ui_.setupUi(this); theme_ = new QtChatTheme(""); idCounter_ = 0; delete ui_.conversation_; conversation_ = new QtChatView(theme_, this); QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); sizePolicy.setHorizontalStretch(80); sizePolicy.setVerticalStretch(0); conversation_->setSizePolicy(sizePolicy); ui_.conversation_ = conversation_; ui_.bottomLayout_->addWidget(conversation_); delete ui_.conversationRoster_; conversationRoster_ = new QtTreeWidget(eventStream, settings, this); QSizePolicy sizePolicy2(QSizePolicy::Preferred, QSizePolicy::Expanding); sizePolicy2.setVerticalStretch(80); conversationRoster_->setSizePolicy(sizePolicy2); ui_.conversationRoster_ = conversationRoster_; ui_.bottomLeftLayout_->setDirection(QBoxLayout::BottomToTop); ui_.bottomLeftLayout_->addWidget(conversationRoster_); setWindowTitle(tr("History")); conversationRoster_->onSomethingSelectedChanged.connect(boost::bind(&QtHistoryWindow::handleSomethingSelectedChanged, this, _1)); connect(conversation_, SIGNAL(scrollRequested(int)), this, SLOT(handleScrollRequested(int))); connect(conversation_, SIGNAL(scrollReachedTop()), this, SLOT(handleScrollReachedTop())); connect(conversation_, SIGNAL(scrollReachedBottom()), this, SLOT(handleScrollReachedBottom())); } QtHistoryWindow::~QtHistoryWindow() { disconnect(conversation_, SIGNAL(scrollRequested(int)), this, SLOT(handleScrollRequested(int))); delete theme_; delete conversation_; // TODO: delete ui_ } void QtHistoryWindow::activate() { emit wantsToActivate(); } void QtHistoryWindow::showEvent(QShowEvent* event) { emit windowOpening(); emit titleUpdated(); QWidget::showEvent(event); } void QtHistoryWindow::closeEvent(QCloseEvent* event) { emit windowClosing(); event->accept(); } void QtHistoryWindow::setRosterModel(Roster* model) { conversationRoster_->setRosterModel(model); } void QtHistoryWindow::addMessage(const std::string &message, const std::string &senderName, bool senderIsSelf, const std::string& avatarPath, const boost::posix_time::ptime& time) { QString scaledAvatarPath = QtScaledAvatarCache(32).getScaledAvatarPath(avatarPath.c_str()); QString messageHTML(P2QSTRING(message)); messageHTML = Qt::escape(messageHTML); QDateTime qTime = B2QDATE(time); QDate date = qTime.date(); std::string id = "id" + boost::lexical_cast(idCounter_++); QString qAvatarPath = scaledAvatarPath.isEmpty() ? "qrc:/icons/avatar.png" : QUrl::fromLocalFile(scaledAvatarPath).toEncoded(); conversation_->addMessageBottom(boost::shared_ptr(new MessageSnippet(messageHTML, Qt::escape(P2QSTRING(senderName)), qTime, qAvatarPath, senderIsSelf, false, theme_, P2QSTRING(id)))); // keep track of the days viewable in the chatView if (!dates_.count(date)) { dates_.insert(date); } } void QtHistoryWindow::handleSomethingSelectedChanged(RosterItem* item) { onSelectedContactChanged(item); } void QtHistoryWindow::resetConversationView() { dates_.clear(); conversation_->resetView(); } void QtHistoryWindow::handleScrollRequested(int pos) { // first message starts with offset 5 if (pos < 5) { pos = 5; } QDate currentDate; foreach (const QDate& date, dates_) { int snippetPosition = conversation_->getSnippetPositionByDate(date); if (snippetPosition <= pos) { currentDate = date; } } if (currentDate_ != currentDate) { currentDate_ = currentDate; ui_.calendarWidget_->setSelectedDate(currentDate_); } } void QtHistoryWindow::handleScrollReachedTop() { } void QtHistoryWindow::handleScrollReachedBottom() { } }