LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
stddatafiltermenucreator.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
31 #include <QVariant>
32 #include <QMenu>
33 #include <interfaces/iinfo.h>
34 #include <interfaces/idatafilter.h>
36 #include "util.h"
37 
38 namespace LeechCraft
39 {
40 namespace Util
41 {
42  namespace
43  {
44  struct DataFilterActionInfo
45  {
46  Entity Entity_;
47  QObject *Plugin_;
48  QByteArray VarID_;
49  };
50  }
51 }
52 }
53 
54 Q_DECLARE_METATYPE (LeechCraft::Util::DataFilterActionInfo)
55 
56 namespace LeechCraft
57 {
58 namespace Util
59 {
60  StdDataFilterMenuCreator::StdDataFilterMenuCreator (const QVariant& dataVar, IEntityManager *em, QMenu *menu)
61  : QObject (menu)
62  , EntityMgr_ (em)
63  {
64  auto entity = MakeEntity (dataVar,
65  QString (),
67  "x-leechcraft/data-filter-request");
68  for (auto plugin : em->GetPossibleHandlers (entity))
69  {
70  auto ii = qobject_cast<IInfo*> (plugin);
71  auto idf = qobject_cast<IDataFilter*> (plugin);
72  if (!idf)
73  continue;
74 
75  const auto& vars = idf->GetFilterVariants ();
76 
77  if (vars.isEmpty ())
78  continue;
79 
80  auto searchMenu = menu->addMenu (ii->GetIcon (), idf->GetFilterVerb ());
81  searchMenu->menuAction ()->setIcon (ii->GetIcon ());
82  for (const auto& var : vars)
83  {
84  auto act = searchMenu->addAction (var.Name_);
85  const DataFilterActionInfo info =
86  {
87  entity,
88  plugin,
89  var.ID_
90  };
91  act->setData (QVariant::fromValue (info));
92  connect (act,
93  SIGNAL (triggered ()),
94  this,
95  SLOT (handleDataFilterAction ()));
96  }
97  }
98  }
99 
100  const QByteArray& StdDataFilterMenuCreator::GetChosenPlugin () const
101  {
102  return ChosenPlugin_;
103  }
104 
106  {
107  return ChosenVariant_;
108  }
109 
110  void StdDataFilterMenuCreator::handleDataFilterAction ()
111  {
112  auto action = qobject_cast<QAction*> (sender ());
113  const auto& data = action->data ().value<DataFilterActionInfo> ();
114 
115  auto entity = data.Entity_;
116  entity.Additional_ ["DataFilter"] = data.VarID_;
117  EntityMgr_->HandleEntity (entity, data.Plugin_);
118 
119  ChosenPlugin_ = qobject_cast<IInfo*> (data.Plugin_)->GetUniqueID ();
120  ChosenVariant_ = data.VarID_;
121  }
122 }
123 }
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:101
Proxy to core entity manager.
virtual QList< FilterVariant > GetFilterVariants() const =0
Returns the list of concrete data filter variants.
virtual bool HandleEntity(LeechCraft::Entity entity, QObject *desired=0)=0
Handles the given entity.
Entity Entity_
Base interface for data filter plugins.
Definition: idatafilter.h:90
QObject * Plugin_
virtual QList< QObject * > GetPossibleHandlers(const LeechCraft::Entity &entity)=0
Queries what plugins can handle the given entity.
Q_DECLARE_METATYPE(LeechCraft::ANFieldData)
QByteArray VarID_
Required interface for every plugin.
Definition: iinfo.h:68