LeechCraft  0.6.70-13729-g7046a9d2a7
Modular cross-platform feature rich live environment.
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 <util/sll/slotclosure.h>
34 #include <interfaces/iinfo.h>
35 #include <interfaces/idatafilter.h>
37 #include "util.h"
38 
39 namespace LC
40 {
41 namespace Util
42 {
43  namespace
44  {
45  template<typename T>
46  void AddDatafilterMenuItem (const IDataFilter::FilterVariant& var, QMenu *menu, T actor)
47  {
48  const auto act = menu->addAction (var.Icon_, var.Name_);
49  new Util::SlotClosure<Util::DeleteLaterPolicy>
50  {
51  [var, actor] () mutable { actor (var); },
52  act,
53  SIGNAL (triggered ()),
54  act
55  };
56  }
57  }
58 
59  StdDataFilterMenuCreator::StdDataFilterMenuCreator (const QVariant& dataVar, IEntityManager *em, QMenu *menu)
60  : QObject (menu)
61  , EntityMgr_ (em)
62  {
63  auto entity = MakeEntity (dataVar,
64  QString (),
66  "x-leechcraft/data-filter-request");
67  for (auto plugin : em->GetPossibleHandlers (entity))
68  {
69  auto ii = qobject_cast<IInfo*> (plugin);
70  auto idf = qobject_cast<IDataFilter*> (plugin);
71  if (!idf)
72  continue;
73 
74  const auto& vars = idf->GetFilterVariants (dataVar);
75 
76  if (vars.isEmpty ())
77  continue;
78 
79  const auto actor = [this, entity, plugin] (const IDataFilter::FilterVariant& var) mutable
80  {
81  entity.Additional_ ["DataFilter"] = var.ID_;
82  EntityMgr_->HandleEntity (entity, plugin);
83 
84  ChosenPlugin_ = qobject_cast<IInfo*> (plugin)->GetUniqueID ();
85  ChosenVariant_ = var.ID_;
86  };
87 
88  if (vars.size () == 1)
89  AddDatafilterMenuItem (vars.value (0), menu, actor);
90  else
91  {
92  auto searchMenu = menu->addMenu (ii->GetIcon (), idf->GetFilterVerb ());
93  for (const auto& var : vars)
94  AddDatafilterMenuItem (var, searchMenu, actor);
95  }
96  }
97  }
98 
99  const QByteArray& StdDataFilterMenuCreator::GetChosenPlugin () const
100  {
101  return ChosenPlugin_;
102  }
103 
105  {
106  return ChosenVariant_;
107  }
108 }
109 }
Proxy to core entity manager.
StdDataFilterMenuCreator(const QVariant &data, IEntityManager *iem, QMenu *menu)
Constructs the StdDataFilterMenuCreator.
virtual QList< QObject * > GetPossibleHandlers(const LC::Entity &entity)=0
Queries what plugins can handle the given entity.
virtual QList< FilterVariant > GetFilterVariants(const QVariant &data) const =0
Returns the list of concrete data filter variants.
Base interface for data filter plugins.
Definition: idatafilter.h:90
virtual bool HandleEntity(LC::Entity entity, QObject *desired=nullptr)=0
Handles the given entity.
QString Name_
The human-readable name of the filter variant.
Definition: idatafilter.h:107
const QByteArray & GetChosenVariant() const
Returns the ID of the chosen data filter variant.
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:105
const QByteArray & GetChosenPlugin() const
Returns the ID of the chosen plugin.
QIcon Icon_
The icon representing the filter variant.
Definition: idatafilter.h:115
QByteArray ID_
The ID of this filter variant.
Definition: idatafilter.h:103
Definition: constants.h:35
Describes a single filter variant supported by this data filter.
Definition: idatafilter.h:96
Required interface for every plugin.
Definition: iinfo.h:68