/src/app/openstrategies.cpp

https://github.com/ABBAPOH/andromeda · C++ · 233 lines · 136 code · 32 blank · 65 comment · 15 complexity · 0d830b4dcde55f2b1c61eb92156bcbae MD5 · raw file

  1. #include "openstrategies.h"
  2. #include <QtCore/QProcess>
  3. #include <QApplication>
  4. #include <Parts/AbstractDocumentFactory>
  5. #include <Parts/constants.h>
  6. #include <Parts/DocumentManager>
  7. #include "browserwindow.h"
  8. using namespace Parts;
  9. using namespace Andromeda;
  10. /*!
  11. \class Andromeda::OpenCurrentWindowStrategy
  12. Opens urls in current window.
  13. */
  14. OpenCurrentWindowStrategy::OpenCurrentWindowStrategy(QObject *parent) :
  15. OpenStrategy(Constants::Actions::Open, parent)
  16. {
  17. }
  18. /*!
  19. \reimp
  20. */
  21. bool OpenCurrentWindowStrategy::canOpen(const QList<QUrl> &urls)
  22. {
  23. return !urls.isEmpty();
  24. }
  25. #include <QtCore/QFileInfo>
  26. #include <QtGui/QDesktopServices>
  27. /*!
  28. \reimp
  29. */
  30. void OpenCurrentWindowStrategy::open(const QList<QUrl> &urls)
  31. {
  32. QList<QUrl> folders;
  33. foreach (const QUrl &url, urls) {
  34. if (!url.isLocalFile()) {
  35. folders.append(url);
  36. continue;
  37. }
  38. Q_ASSERT(url.isLocalFile());
  39. QString path = url.toLocalFile();
  40. QFileInfo info(path);
  41. if (info.isDir() && !info.isBundle()) {
  42. folders.append(url);
  43. } else {
  44. #ifdef Q_OS_LINUX
  45. if (info.isExecutable()) {
  46. QProcess::startDetached(path);
  47. return;
  48. }
  49. #endif
  50. // TODO: allow to open default editor instead
  51. QDesktopServices::openUrl(url);
  52. }
  53. }
  54. if (folders.isEmpty())
  55. return;
  56. BrowserWindow *m_activeWindow = qobject_cast<BrowserWindow *>(QApplication::activeWindow());
  57. if (!m_activeWindow)
  58. m_activeWindow = new BrowserWindow;
  59. // TODO: open files in external program
  60. if (folders.count() == 1)
  61. m_activeWindow->open(folders.first());
  62. else // TODO: switch between new tabs/windows
  63. m_activeWindow->openNewTabs(folders);
  64. m_activeWindow->show();
  65. }
  66. /*!
  67. \reimp
  68. */
  69. QString OpenCurrentWindowStrategy::text() const
  70. {
  71. return tr("Open");
  72. }
  73. /*!
  74. \class Andromeda::OpenNewTabStrategy
  75. Opens urls in new tabs in current window.
  76. */
  77. OpenNewTabStrategy::OpenNewTabStrategy(QObject *parent) :
  78. OpenStrategy(Constants::Actions::OpenInTab, parent)
  79. {
  80. }
  81. /*!
  82. \reimp
  83. */
  84. bool OpenNewTabStrategy::canOpen(const QList<QUrl> &urls)
  85. {
  86. return !urls.isEmpty();
  87. }
  88. /*!
  89. \reimp
  90. */
  91. void OpenNewTabStrategy::open(const QList<QUrl> &urls)
  92. {
  93. BrowserWindow *m_activeWindow = qobject_cast<BrowserWindow *>(QApplication::activeWindow());
  94. if (!m_activeWindow)
  95. m_activeWindow = new BrowserWindow;
  96. if (urls.count() == 1)
  97. m_activeWindow->openNewTab(urls.first());
  98. else
  99. m_activeWindow->openNewTabs(urls);
  100. m_activeWindow->show();
  101. }
  102. /*!
  103. \reimp
  104. */
  105. QString OpenNewTabStrategy::text() const
  106. {
  107. return tr("Open in tab");
  108. }
  109. /*!
  110. \reimp
  111. */
  112. Qt::KeyboardModifiers OpenNewTabStrategy::modifiers() const
  113. {
  114. return Qt::AltModifier;
  115. }
  116. /*!
  117. \class Andromeda::OpenNewWindowStrategy
  118. Opens urls in new windows.
  119. */
  120. OpenNewWindowStrategy::OpenNewWindowStrategy(QObject *parent) :
  121. OpenStrategy(Constants::Actions::OpenInWindow, parent)
  122. {
  123. }
  124. /*!
  125. \reimp
  126. */
  127. bool OpenNewWindowStrategy::canOpen(const QList<QUrl> &urls)
  128. {
  129. return !urls.isEmpty();
  130. }
  131. /*!
  132. \reimp
  133. */
  134. void OpenNewWindowStrategy::open(const QList<QUrl> &urls)
  135. {
  136. foreach (const QUrl &url, urls) {
  137. BrowserWindow *window = new BrowserWindow;
  138. window->open(url);
  139. window->show();
  140. }
  141. }
  142. /*!
  143. \reimp
  144. */
  145. QString OpenNewWindowStrategy::text() const
  146. {
  147. return tr("Open in window");
  148. }
  149. /*!
  150. \reimp
  151. */
  152. Qt::KeyboardModifiers OpenNewWindowStrategy::modifiers() const
  153. {
  154. return Qt::ControlModifier | Qt::AltModifier;
  155. }
  156. /*!
  157. \class Andromeda::OpenEditorStrategy
  158. Opens urls in an editor.
  159. */
  160. OpenEditorStrategy::OpenEditorStrategy(QObject *parent):
  161. OpenStrategy("OpenEditor", parent)
  162. {
  163. }
  164. /*!
  165. \reimp
  166. */
  167. bool OpenEditorStrategy::canOpen(const QList<QUrl> &urls)
  168. {
  169. // TODO: return false for directories
  170. return !urls.isEmpty();
  171. }
  172. /*!
  173. \reimp
  174. */
  175. void OpenEditorStrategy::open(const QList<QUrl> &urls)
  176. {
  177. DocumentManager *manager = DocumentManager::instance();
  178. QList<AbstractDocumentFactory *> factories = manager->factoriesForUrls(urls);
  179. if (factories.isEmpty())
  180. return;
  181. BrowserWindow *m_activeWindow = qobject_cast<BrowserWindow *>(QApplication::activeWindow());
  182. if (!m_activeWindow)
  183. m_activeWindow = new BrowserWindow;
  184. // TODO: handle multiple editors via dialog
  185. m_activeWindow->openEditor(urls, factories.first()->id());
  186. m_activeWindow->show();
  187. }
  188. /*!
  189. \reimp
  190. */
  191. QString OpenEditorStrategy::text() const
  192. {
  193. return tr("Open in editor");
  194. }
  195. /*!
  196. \reimp
  197. */
  198. QKeySequence OpenEditorStrategy::keySequence() const
  199. {
  200. return QKeySequence("Ctrl+E");
  201. }