diff options
author | Tobias Markmann <tm@ayena.de> | 2014-09-29 08:07:05 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2014-10-17 20:11:30 (GMT) |
commit | 38b0cb785fea8eae5e48fae56440695fdfd10ee1 (patch) | |
tree | 78bb8ce9a6bcbc397805bcd433b96b3b23994f09 /Swift/QtUI/QtChatWindow.cpp | |
parent | 1722d220533a78e8b2acbcd571631960656e78f8 (diff) | |
download | swift-contrib-38b0cb785fea8eae5e48fae56440695fdfd10ee1.zip swift-contrib-38b0cb785fea8eae5e48fae56440695fdfd10ee1.tar.bz2 |
Disable online only actions when offline.
Disabling action, menu items and drag 'n' drop which require an online
connection when the user is offline.
Test-Information:
Checked by going offline and checking the relevant actions and menu items.
Change-Id: Iacfa2c9f815d3b9bbad9ca4c2d0d04f95ce9a9e4
Diffstat (limited to 'Swift/QtUI/QtChatWindow.cpp')
-rw-r--r-- | Swift/QtUI/QtChatWindow.cpp | 88 |
1 files changed, 62 insertions, 26 deletions
diff --git a/Swift/QtUI/QtChatWindow.cpp b/Swift/QtUI/QtChatWindow.cpp index ed79dcc..68104b4 100644 --- a/Swift/QtUI/QtChatWindow.cpp +++ b/Swift/QtUI/QtChatWindow.cpp @@ -227,142 +227,148 @@ QtChatWindow::AlertID QtChatWindow::addAlert(const std::string& alertText) { alertLayout->addWidget(closeButton); QPalette palette = alertWidget->palette(); palette.setColor(QPalette::Window, QColor(Qt::yellow)); palette.setColor(QPalette::WindowText, QColor(Qt::black)); alertWidget->setStyleSheet(alertStyleSheet_); alertLabel->setStyleSheet(alertStyleSheet_); AlertID id = nextAlertId_++; alertWidgets_[id] = alertWidget; return id; } void QtChatWindow::removeAlert(const AlertID id) { std::map<AlertID, QWidget*>::iterator i = alertWidgets_.find(id); if (i != alertWidgets_.end()) { alertLayout_->removeWidget(i->second); delete i->second; alertWidgets_.erase(i); } } void QtChatWindow::setTabComplete(TabComplete* completer) { completer_ = completer; } void QtChatWindow::handleKeyPressEvent(QKeyEvent* event) { event->ignore(); if (event->isAccepted()) { return; } event->accept(); int key = event->key(); if (key == Qt::Key_Tab) { tabComplete(); - } else if ((key == Qt::Key_Up) && input_->toPlainText().isEmpty() && !(lastSentMessage_.isEmpty())) { + } + else if ((key == Qt::Key_Up) && input_->toPlainText().isEmpty() && !(lastSentMessage_.isEmpty())) { beginCorrection(); - } else if (key == Qt::Key_Down && isCorrection_ && input_->textCursor().atBlockEnd()) { + } + else if (key == Qt::Key_Down && isCorrection_ && input_->textCursor().atBlockEnd()) { cancelCorrection(); - } else if (key == Qt::Key_Down || key == Qt::Key_Up) { + } + else if (key == Qt::Key_Down || key == Qt::Key_Up) { /* Drop */ - } else { + } + else { messageLog_->handleKeyPressEvent(event); } } void QtChatWindow::beginCorrection() { boost::optional<AlertID> newCorrectingAlert; if (correctionEnabled_ == ChatWindow::Maybe) { newCorrectingAlert = addAlert(Q2PSTRING(tr("This chat may not support message correction. If you send a correction anyway, it may appear as a duplicate message"))); - } else if (correctionEnabled_ == ChatWindow::No) { + } + else if (correctionEnabled_ == ChatWindow::No) { newCorrectingAlert = addAlert(Q2PSTRING(tr("This chat does not support message correction. If you send a correction anyway, it will appear as a duplicate message"))); } if (newCorrectingAlert) { if (correctingAlert_) { removeAlert(*correctingAlert_); } correctingAlert_ = newCorrectingAlert; } QTextCursor cursor = input_->textCursor(); cursor.select(QTextCursor::Document); cursor.beginEditBlock(); cursor.insertText(QString(lastSentMessage_)); cursor.endEditBlock(); isCorrection_ = true; correctingLabel_->show(); input_->setStyleSheet(alertStyleSheet_); labelsWidget_->setEnabled(false); } void QtChatWindow::cancelCorrection() { if (correctingAlert_) { removeAlert(*correctingAlert_); correctingAlert_.reset(); } QTextCursor cursor = input_->textCursor(); cursor.select(QTextCursor::Document); cursor.removeSelectedText(); isCorrection_ = false; correctingLabel_->hide(); input_->setStyleSheet(qApp->styleSheet()); labelsWidget_->setEnabled(true); } QByteArray QtChatWindow::getSplitterState() { return logRosterSplitter_->saveState(); } void QtChatWindow::handleChangeSplitterState(QByteArray state) { logRosterSplitter_->restoreState(state); } void QtChatWindow::handleSplitterMoved(int, int) { emit splitterMoved(); } void QtChatWindow::tabComplete() { if (!completer_) { return; } QTextCursor cursor; if (tabCompleteCursor_.hasSelection()) { cursor = tabCompleteCursor_; - } else { + } + else { cursor = input_->textCursor(); while(cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor) && cursor.document()->characterAt(cursor.position() - 1) != ' ') { } } QString root = cursor.selectedText(); if (root.isEmpty()) { return; } QString suggestion = P2QSTRING(completer_->completeWord(Q2PSTRING(root))); if (root == suggestion) { return; } tabCompletion_ = true; cursor.beginEditBlock(); cursor.removeSelectedText(); int oldPosition = cursor.position(); cursor.insertText(suggestion); tabCompleteCursor_ = cursor; tabCompleteCursor_.setPosition(oldPosition, QTextCursor::KeepAnchor); cursor.endEditBlock(); tabCompletion_ = false; } void QtChatWindow::setRosterModel(Roster* roster) { treeWidget_->setRosterModel(roster); } void QtChatWindow::setAvailableSecurityLabels(const std::vector<SecurityLabelsCatalog::Item>& labels) { delete labelModel_; labelModel_ = new LabelModel(); labelModel_->availableLabels_ = labels; int i = 0; int defaultIndex = 0; labelsWidget_->setModel(labelModel_); @@ -374,71 +380,72 @@ void QtChatWindow::setAvailableSecurityLabels(const std::vector<SecurityLabelsCa i++; } labelsWidget_->setCurrentIndex(defaultIndex); } void QtChatWindow::handleCurrentLabelChanged(int index) { if (static_cast<size_t>(index) >= labelModel_->availableLabels_.size()) { qDebug() << "User selected a label that doesn't exist"; return; } const SecurityLabelsCatalog::Item& label = labelModel_->availableLabels_[index]; if (label.getLabel()) { QPalette palette = labelsWidget_->palette(); //palette.setColor(QPalette::Base, P2QSTRING(label.getLabel()->getBackgroundColor())); palette.setColor(labelsWidget_->backgroundRole(), P2QSTRING(label.getLabel()->getBackgroundColor())); palette.setColor(labelsWidget_->foregroundRole(), P2QSTRING(label.getLabel()->getForegroundColor())); labelsWidget_->setPalette(palette); midBar_->setPalette(palette); labelsWidget_->setAutoFillBackground(true); } else { labelsWidget_->setAutoFillBackground(false); labelsWidget_->setPalette(defaultLabelsPalette_); midBar_->setPalette(defaultLabelsPalette_); } } void QtChatWindow::setSecurityLabelsError() { labelsWidget_->setEnabled(false); } void QtChatWindow::setSecurityLabelsEnabled(bool enabled) { if (enabled) { labelsWidget_->setEnabled(true); labelsWidget_->show(); - } else { + } + else { labelsWidget_->hide(); } } void QtChatWindow::setCorrectionEnabled(Tristate enabled) { correctionEnabled_ = enabled; } void QtChatWindow::setFileTransferEnabled(Tristate enabled) { fileTransferEnabled_ = enabled; } SecurityLabelsCatalog::Item QtChatWindow::getSelectedSecurityLabel() { assert(labelsWidget_->isEnabled()); assert(labelsWidget_->currentIndex() >= 0 && static_cast<size_t>(labelsWidget_->currentIndex()) < labelModel_->availableLabels_.size()); return labelModel_->availableLabels_[labelsWidget_->currentIndex()]; } void QtChatWindow::closeEvent(QCloseEvent* event) { event->accept(); emit windowClosing(); onClosed(); } void QtChatWindow::convertToMUC(MUCType mucType) { impromptu_ = (mucType == ImpromptuMUC); treeWidget_->setMessageTarget(impromptu_ ? QtTreeWidget::MessageDisplayJID : QtTreeWidget::MessageDefaultJID); isMUC_ = true; treeWidget_->show(); subject_->setVisible(!impromptu_); } void QtChatWindow::qAppFocusChanged(QWidget* /*old*/, QWidget* /*now*/) { if (isWidgetSelected()) { lastLineTracker_.setHasFocus(true); @@ -466,281 +473,310 @@ void QtChatWindow::showEvent(QShowEvent* event) { emit windowOpening(); QWidget::showEvent(event); } void QtChatWindow::setUnreadMessageCount(int count) { if (unreadCount_ != count) { unreadCount_ = count; updateTitleWithUnreadCount(); emit countUpdated(); } } void QtChatWindow::setContactChatState(ChatState::ChatStateType state) { contactIsTyping_ = (state == ChatState::Composing); emit titleUpdated(); } QtTabbable::AlertType QtChatWindow::getWidgetAlertState() { if (contactIsTyping_) { return ImpendingActivity; } if (unreadCount_ > 0) { return WaitingActivity; } return NoActivity; } void QtChatWindow::setName(const std::string& name) { contact_ = P2QSTRING(name); updateTitleWithUnreadCount(); } void QtChatWindow::updateTitleWithUnreadCount() { if (isWindow()) { setWindowTitle(unreadCount_ > 0 ? QString("(%1) %2").arg(unreadCount_).arg(contact_) : contact_); - } else { + } + else { setWindowTitle(contact_); } emit titleUpdated(); } void QtChatWindow::flash() { emit requestFlash(); } int QtChatWindow::getCount() { return unreadCount_; } void QtChatWindow::returnPressed() { if (!inputEnabled_) { return; } messageLog_->scrollToBottom(); lastSentMessage_ = QString(input_->toPlainText()); onSendMessageRequest(Q2PSTRING(input_->toPlainText()), isCorrection_); inputClearing_ = true; input_->clear(); cancelCorrection(); inputClearing_ = false; } void QtChatWindow::handleInputChanged() { if (inputClearing_) { return; } if (input_->toPlainText().isEmpty()) { onUserCancelsTyping(); - } else { + } + else { onUserTyping(); } } void QtChatWindow::handleCursorPositionChanged() { if (tabCompletion_) { return; } tabCompleteCursor_.clearSelection(); } void QtChatWindow::show() { if (parentWidget() == NULL) { QWidget::show(); } emit windowOpening(); } bool QtChatWindow::isVisible() const { return QWidget::isVisible(); } void QtChatWindow::activate() { if (isWindow()) { QWidget::show(); } emit wantsToActivate(); input_->setFocus(); } void QtChatWindow::resizeEvent(QResizeEvent*) { emit geometryChanged(); } void QtChatWindow::moveEvent(QMoveEvent*) { emit geometryChanged(); } void QtChatWindow::dragEnterEvent(QDragEnterEvent *event) { - if (event->mimeData()->hasUrls() && event->mimeData()->urls().size() == 1) { - // TODO: check whether contact actually supports file transfer - if (!isMUC_) { - event->acceptProposedAction(); + if (inputEnabled_) { + if (event->mimeData()->hasUrls() && event->mimeData()->urls().size() == 1) { + // TODO: check whether contact actually supports file transfer + if (!isMUC_) { + event->acceptProposedAction(); + } } - } else if (event->mimeData()->hasFormat("application/vnd.swift.contact-jid-list")) { - if (isMUC_ || supportsImpromptuChat_) { - event->acceptProposedAction(); + else if (event->mimeData()->hasFormat("application/vnd.swift.contact-jid-list")) { + if (isMUC_ || supportsImpromptuChat_) { + event->acceptProposedAction(); + } } } } void QtChatWindow::dropEvent(QDropEvent *event) { if (fileTransferEnabled_ == ChatWindow::Yes && event->mimeData()->hasUrls()) { if (event->mimeData()->urls().size() == 1) { onSendFileRequest(Q2PSTRING(event->mimeData()->urls().at(0).toLocalFile())); - } else { + } + else { std::string messageText(Q2PSTRING(tr("Sending of multiple files at once isn't supported at this time."))); ChatMessage message; message.append(boost::make_shared<ChatTextMessagePart>(messageText)); addSystemMessage(message, DefaultDirection); } - } else if (event->mimeData()->hasFormat("application/vnd.swift.contact-jid-list")) { + } + else if (event->mimeData()->hasFormat("application/vnd.swift.contact-jid-list")) { QByteArray dataBytes = event->mimeData()->data("application/vnd.swift.contact-jid-list"); QDataStream dataStream(&dataBytes, QIODevice::ReadOnly); std::vector<JID> invites; while (!dataStream.atEnd()) { QString jidString; dataStream >> jidString; invites.push_back(Q2PSTRING(jidString)); } onInviteToChat(invites); } } void QtChatWindow::setAvailableOccupantActions(const std::vector<OccupantAction>& actions) { treeWidget_->setAvailableOccupantActions(actions); } void QtChatWindow::setSubject(const std::string& subject) { //subject_->setVisible(!subject.empty()); subject_->setText(P2QSTRING(subject)); subject_->setToolTip(P2QSTRING(subject)); subject_->setCursorPosition(0); } void QtChatWindow::handleActionButtonClicked() { QMenu contextMenu; QAction* changeSubject = NULL; QAction* configure = NULL; QAction* affiliations = NULL; QAction* destroy = NULL; QAction* invite = NULL; QAction* block = NULL; QAction* unblock = NULL; if (availableRoomActions_.empty()) { if (blockingState_ == IsBlocked) { unblock = contextMenu.addAction(tr("Unblock")); - } else if (blockingState_ == IsUnblocked) { + unblock->setEnabled(inputEnabled_); + } + else if (blockingState_ == IsUnblocked) { block = contextMenu.addAction(tr("Block")); + block->setEnabled(inputEnabled_); } if (supportsImpromptuChat_) { invite = contextMenu.addAction(tr("Invite person to this chat…")); + invite->setEnabled(inputEnabled_); } - } else { + } + else { foreach(ChatWindow::RoomAction availableAction, availableRoomActions_) { if (impromptu_) { // hide options we don't need in impromptu chats if (availableAction == ChatWindow::ChangeSubject || availableAction == ChatWindow::Configure || availableAction == ChatWindow::Affiliations || availableAction == ChatWindow::Destroy) { continue; } } switch(availableAction) { - case ChatWindow::ChangeSubject: changeSubject = contextMenu.addAction(tr("Change subject…")); break; - case ChatWindow::Configure: configure = contextMenu.addAction(tr("Configure room…")); break; - case ChatWindow::Affiliations: affiliations = contextMenu.addAction(tr("Edit affiliations…")); break; - case ChatWindow::Destroy: destroy = contextMenu.addAction(tr("Destroy room")); break; - case ChatWindow::Invite: invite = contextMenu.addAction(tr("Invite person to this room…")); break; + case ChatWindow::ChangeSubject: + changeSubject = contextMenu.addAction(tr("Change subject…")); + changeSubject->setEnabled(inputEnabled_); + break; + case ChatWindow::Configure: + configure = contextMenu.addAction(tr("Configure room…")); + configure->setEnabled(inputEnabled_); + break; + case ChatWindow::Affiliations: + affiliations = contextMenu.addAction(tr("Edit affiliations…")); + affiliations->setEnabled(inputEnabled_); + break; + case ChatWindow::Destroy: + destroy = contextMenu.addAction(tr("Destroy room")); + destroy->setEnabled(inputEnabled_); + break; + case ChatWindow::Invite: + invite = contextMenu.addAction(tr("Invite person to this room…")); + invite->setEnabled(inputEnabled_); + break; } } } QAction* bookmark = contextMenu.addAction(tr("Add boomark...")); + bookmark->setEnabled(inputEnabled_); QAction* result = contextMenu.exec(QCursor::pos()); if (result == NULL) { /* Skip processing. Note that otherwise, because the actions could be null they could match */ } else if (result == changeSubject) { bool ok; QString subject = QInputDialog::getText(this, tr("Change room subject"), tr("New subject:"), QLineEdit::Normal, subject_->text(), &ok); if (ok) { onChangeSubjectRequest(Q2PSTRING(subject)); } } else if (result == configure) { onConfigureRequest(Form::ref()); } else if (result == affiliations) { if (!affiliationEditor_) { onGetAffiliationsRequest(); affiliationEditor_ = new QtAffiliationEditor(this); connect(affiliationEditor_, SIGNAL(accepted()), this, SLOT(handleAffiliationEditorAccepted())); } affiliationEditor_->show(); } else if (result == destroy) { QMessageBox msgBox; msgBox.setWindowTitle(tr("Confirm room destruction")); msgBox.setText(tr("Are you sure you want to destroy the room?")); msgBox.setInformativeText(tr("This will destroy the room.")); msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); msgBox.setDefaultButton(QMessageBox::No); if (msgBox.exec() == QMessageBox::Yes) { onDestroyRequest(); } } else if (result == invite) { onInviteToChat(std::vector<JID>()); } else if (result == block) { onBlockUserRequest(); } else if (result == unblock) { onUnblockUserRequest(); - } else if (result == bookmark) { + } + else if (result == bookmark) { onBookmarkRequest(); } } void QtChatWindow::handleAffiliationEditorAccepted() { onChangeAffiliationsRequest(affiliationEditor_->getChanges()); } void QtChatWindow::setAffiliations(MUCOccupant::Affiliation affiliation, const std::vector<JID>& jids) { if (!affiliationEditor_) return; affiliationEditor_->setAffiliations(affiliation, jids); } void QtChatWindow::setAvailableRoomActions(const std::vector<RoomAction>& actions) { availableRoomActions_ = actions; } void QtChatWindow::setBlockingState(BlockingState state) { blockingState_ = state; } void QtChatWindow::setCanInitiateImpromptuChats(bool supportsImpromptu) { supportsImpromptuChat_ = supportsImpromptu; } void QtChatWindow::showBookmarkWindow(const MUCBookmark& bookmark) { QtAddBookmarkWindow* window = new QtAddBookmarkWindow(eventStream_, bookmark); window->show(); } void QtChatWindow::showRoomConfigurationForm(Form::ref form) { if (mucConfigurationWindow_) { delete mucConfigurationWindow_.data(); } mucConfigurationWindow_ = new QtMUCConfigurationWindow(form); |