From a27867ae2bb0f6157d8c26a8f65425e10d57bff9 Mon Sep 17 00:00:00 2001
From: sledgehammer999 <hammered999@gmail.com>
Date: Sat, 1 Feb 2014 14:02:37 +0200
Subject: [PATCH] Bring dialog boxes in the front when qbt doesn't have focus.

Code inspired by glassez patch.
---
 src/addnewtorrentdialog.cpp | 20 ++++++------
 src/messageboxraised.cpp    | 63 +++++++++++++++++++++++++++++++++++++
 src/messageboxraised.h      | 59 ++++++++++++++++++++++++++++++++++
 src/src.pro                 |  6 ++--
 4 files changed, 136 insertions(+), 12 deletions(-)
 create mode 100644 src/messageboxraised.cpp
 create mode 100644 src/messageboxraised.h

diff --git a/src/addnewtorrentdialog.cpp b/src/addnewtorrentdialog.cpp
index 0e360db89..f61375f9d 100644
--- a/src/addnewtorrentdialog.cpp
+++ b/src/addnewtorrentdialog.cpp
@@ -40,12 +40,12 @@
 #include "iconprovider.h"
 #include "fs_utils.h"
 #include "autoexpandabledialog.h"
+#include "messageboxraised.h"
 
 #include <QString>
 #include <QFile>
 #include <QUrl>
 #include <QMenu>
-#include <QMessageBox>
 #include <QTimer>
 #include <QFileDialog>
 #include <libtorrent/version.hpp>
@@ -185,7 +185,7 @@ bool AddNewTorrentDialog::loadTorrent(const QString& torrent_path, const QString
     m_filePath = torrent_path;
 
   if (!QFile::exists(m_filePath)) {
-    QMessageBox::critical(0, tr("I/O Error"), tr("The torrent file does not exist."));
+    MessageBoxRaised::critical(0, tr("I/O Error"), tr("The torrent file does not exist."));
     return false;
   }
 
@@ -195,13 +195,13 @@ bool AddNewTorrentDialog::loadTorrent(const QString& torrent_path, const QString
     m_torrentInfo = new torrent_info(fsutils::toNativePath(m_filePath).toUtf8().data());
     m_hash = misc::toQString(m_torrentInfo->info_hash());
   } catch(const std::exception& e) {
-    QMessageBox::critical(0, tr("Invalid torrent"), tr("Failed to load the torrent: %1").arg(e.what()));
+    MessageBoxRaised::critical(0, tr("Invalid torrent"), tr("Failed to load the torrent: %1").arg(e.what()));
     return false;
   }
 
   // Prevent showing the dialog if download is already present
   if (QBtSession::instance()->getTorrentHandle(m_hash).is_valid()) {
-    QMessageBox::information(0, tr("Already in download list"), tr("Torrent is already in download list. Merging trackers."), QMessageBox::Ok);
+    MessageBoxRaised::information(0, tr("Already in download list"), tr("Torrent is already in download list. Merging trackers."), QMessageBox::Ok);
     QBtSession::instance()->addTorrent(m_filePath, false, m_url);;
     return false;
   }
@@ -261,13 +261,13 @@ bool AddNewTorrentDialog::loadMagnet(const QString &magnet_uri)
   m_url = magnet_uri;
   m_hash = misc::magnetUriToHash(m_url);
   if (m_hash.isEmpty()) {
-    QMessageBox::critical(0, tr("Invalid magnet link"), tr("This magnet link was not recognized"));
+    MessageBoxRaised::critical(0, tr("Invalid magnet link"), tr("This magnet link was not recognized"));
     return false;
   }
 
   // Prevent showing the dialog if download is already present
   if (QBtSession::instance()->getTorrentHandle(m_hash).is_valid()) {
-    QMessageBox::information(0, tr("Already in download list"), tr("Magnet link is already in download list. Merging trackers."), QMessageBox::Ok);
+    MessageBoxRaised::information(0, tr("Already in download list"), tr("Magnet link is already in download list. Merging trackers."), QMessageBox::Ok);
     QBtSession::instance()->addMagnetUri(m_url, false);
     return false;
   }
@@ -431,7 +431,7 @@ void AddNewTorrentDialog::renameSelectedFile()
                                                       index.data().toString(), &ok).trimmed();
   if (ok && !new_name_last.isEmpty()) {
     if (!fsutils::isValidFileSystemName(new_name_last)) {
-      QMessageBox::warning(this, tr("The file could not be renamed"),
+      MessageBoxRaised::warning(this, tr("The file could not be renamed"),
                            tr("This file name contains forbidden characters, please choose a different one."),
                            QMessageBox::Ok);
       return;
@@ -456,7 +456,7 @@ void AddNewTorrentDialog::renameSelectedFile()
         if (i == file_index) continue;
         if (fsutils::sameFileNames(m_filesPath.at(i), new_name)) {
           // Display error message
-          QMessageBox::warning(this, tr("The file could not be renamed"),
+          MessageBoxRaised::warning(this, tr("The file could not be renamed"),
                                tr("This name is already in use in this folder. Please use a different name."),
                                QMessageBox::Ok);
           return;
@@ -490,7 +490,7 @@ void AddNewTorrentDialog::renameSelectedFile()
 #else
         if (current_name.startsWith(new_path, Qt::CaseInsensitive)) {
 #endif
-          QMessageBox::warning(this, tr("The folder could not be renamed"),
+          MessageBoxRaised::warning(this, tr("The folder could not be renamed"),
                                tr("This name is already in use in this folder. Please use a different name."),
                                QMessageBox::Ok);
           return;
@@ -703,7 +703,7 @@ void AddNewTorrentDialog::updateMetadata(const QTorrentHandle &h) {
     setdialogPosition();
     setMetadataProgressIndicator(false, tr("Metadata retrieval complete"));
   } catch (invalid_handle&) {
-    QMessageBox::critical(0, tr("I/O Error"), ("Unknown error."));
+    MessageBoxRaised::critical(0, tr("I/O Error"), ("Unknown error."));
     setMetadataProgressIndicator(false, tr("Unknown error"));
     return;
   }
diff --git a/src/messageboxraised.cpp b/src/messageboxraised.cpp
new file mode 100644
index 000000000..ac427e77f
--- /dev/null
+++ b/src/messageboxraised.cpp
@@ -0,0 +1,63 @@
+/*
+ * Bittorrent Client using Qt4 and libtorrent.
+ * Copyright (C) 2014  sledgehammer999
+ *
+ * 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * In addition, as a special exception, the copyright holders give permission to
+ * link this program with the OpenSSL project's "OpenSSL" library (or with
+ * modified versions of it that use the same license as the "OpenSSL" library),
+ * and distribute the linked executables. You must obey the GNU General Public
+ * License in all respects for all of the code used other than "OpenSSL".  If you
+ * modify file(s), you may extend this exception to your version of the file(s),
+ * but you are not obligated to do so. If you do not wish to do so, delete this
+ * exception statement from your version.
+ *
+ * Contact : hammered999@gmail.com
+ */
+
+#include "messageboxraised.h"
+
+MessageBoxRaised::MessageBoxRaised(QMessageBox::Icon icon, const QString &title, const QString &text,
+                                   QMessageBox::StandardButtons buttons, QWidget *parent, Qt::WindowFlags f)
+  : QMessageBox(icon, title, text, buttons, parent, f) {}
+
+QMessageBox::StandardButton MessageBoxRaised::impl(const QMessageBox::Icon &icon, QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) {
+  MessageBoxRaised dlg(icon, title, text, buttons, parent);
+  dlg.setDefaultButton(defaultButton);
+  return (QMessageBox::StandardButton)dlg.exec();
+}
+
+QMessageBox::StandardButton MessageBoxRaised::critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) {
+  return impl(Critical, parent, title, text, buttons, defaultButton);
+}
+
+QMessageBox::StandardButton MessageBoxRaised::information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) {
+  return impl(Information, parent, title, text, buttons, defaultButton);
+}
+
+QMessageBox::StandardButton MessageBoxRaised::question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) {
+  return impl(Question, parent, title, text, buttons, defaultButton);
+}
+
+QMessageBox::StandardButton MessageBoxRaised::warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton) {
+  return impl(Warning, parent, title, text, buttons, defaultButton);
+}
+
+void MessageBoxRaised::showEvent(QShowEvent *event) {
+  QMessageBox::showEvent(event);
+  activateWindow();
+  raise();
+}
diff --git a/src/messageboxraised.h b/src/messageboxraised.h
new file mode 100644
index 000000000..d9be5ffbd
--- /dev/null
+++ b/src/messageboxraised.h
@@ -0,0 +1,59 @@
+/*
+ * Bittorrent Client using Qt4 and libtorrent.
+ * Copyright (C) 2014  sledgehammer999
+ *
+ * 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * In addition, as a special exception, the copyright holders give permission to
+ * link this program with the OpenSSL project's "OpenSSL" library (or with
+ * modified versions of it that use the same license as the "OpenSSL" library),
+ * and distribute the linked executables. You must obey the GNU General Public
+ * License in all respects for all of the code used other than "OpenSSL".  If you
+ * modify file(s), you may extend this exception to your version of the file(s),
+ * but you are not obligated to do so. If you do not wish to do so, delete this
+ * exception statement from your version.
+ *
+ * Contact : hammered999@gmail.com
+ */
+
+#ifndef MESSAGEBOXRAISED_H
+#define MESSAGEBOXRAISED_H
+
+#include <QMessageBox>
+
+class MessageBoxRaised : public QMessageBox
+{
+  Q_OBJECT
+
+private:
+  MessageBoxRaised(QMessageBox::Icon icon, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = NoButton, QWidget *parent = 0, Qt::WindowFlags f = Qt::Dialog|Qt::MSWindowsFixedSizeDialogHint);
+  MessageBoxRaised();
+  MessageBoxRaised(MessageBoxRaised const&);
+  void operator=(MessageBoxRaised const&);
+
+public:
+  static QMessageBox::StandardButton critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
+  static QMessageBox::StandardButton information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
+  static QMessageBox::StandardButton question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
+  static QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
+
+protected:
+  void showEvent(QShowEvent *event);
+
+private:
+  static QMessageBox::StandardButton impl(const QMessageBox::Icon &icon, QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = QMessageBox::Ok, QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
+};
+
+#endif // MESSAGEBOXRAISED_H
diff --git a/src/src.pro b/src/src.pro
index 01f51f16d..9e9cbcef4 100644
--- a/src/src.pro
+++ b/src/src.pro
@@ -107,7 +107,8 @@ HEADERS += misc.h \
            scannedfoldersmodel.h \
            qinisettings.h \
            smtp.h \
-           dnsupdater.h
+           dnsupdater.h \
+           messageboxraised.h
 
 
 SOURCES += main.cpp \
@@ -116,7 +117,8 @@ SOURCES += main.cpp \
            misc.cpp \
            fs_utils.cpp \
            smtp.cpp \
-           dnsupdater.cpp
+           dnsupdater.cpp \
+           messageboxraised.cpp
 
 nox {
   HEADERS += headlessloader.h