1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2018-07-30
7  * Description : Generic digiKam plugin definition.
8  *
9  * Copyright (C) 2018-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "dplugingeneric.h"
25 
26 // Qt includes
27 
28 #include <QApplication>
29 
30 // Local includes
31 
32 #include "digikam_version.h"
33 #include "digikam_debug.h"
34 #include "dxmlguiwindow.h"
35 #include "dplugindialog.h"
36 
37 namespace Digikam
38 {
39 
40 class Q_DECL_HIDDEN DPluginGeneric::Private
41 {
42 public:
43 
Private()44     explicit Private()
45     {
46     }
47 
48     QList<DPluginAction*> actions;
49 };
50 
DPluginGeneric(QObject * const parent)51 DPluginGeneric::DPluginGeneric(QObject* const parent)
52     : DPlugin(parent),
53       d(new Private)
54 {
55 }
56 
~DPluginGeneric()57 DPluginGeneric::~DPluginGeneric()
58 {
59     delete d;
60 }
61 
infoIface(QObject * const ac) const62 DInfoInterface* DPluginGeneric::infoIface(QObject* const ac) const
63 {
64     DPluginAction* const pac = dynamic_cast<DPluginAction*>(ac);
65 
66     if (pac)
67     {
68         DXmlGuiWindow* const gui = dynamic_cast<DXmlGuiWindow*>(pac->parent());
69 
70         if (gui)
71         {
72             return gui->infoIface(pac);
73         }
74 
75         DInfoInterface* const iface = dynamic_cast<DInfoInterface*>(pac->parent());
76 
77         if (iface)
78         {
79             return iface;
80         }
81     }
82 
83     return nullptr;
84 }
85 
count() const86 int DPluginGeneric::count() const
87 {
88     int count       = 0;
89     QObject* parent = nullptr;
90 
91     foreach (DPluginAction* const ac, d->actions)
92     {
93         if (ac)
94         {
95             // NOTE: we will return the count of actions registered with the same parents,
96             //       as each parent registered the same list of actions through setup().
97 
98             if (!count)
99             {
100                 parent = ac->parent(),
101                 ++count;
102             }
103             else if (ac->parent() == parent)
104             {
105                 ++count;
106             }
107         }
108     }
109 
110     return count;
111 }
112 
setVisible(bool b)113 void DPluginGeneric::setVisible(bool b)
114 {
115     foreach (DPluginAction* const ac, d->actions)
116     {
117         if (ac)
118         {
119             ac->setVisible(b);
120         }
121     }
122 }
123 
actions(QObject * const parent) const124 QList<DPluginAction*> DPluginGeneric::actions(QObject* const parent) const
125 {
126     QList<DPluginAction*> list;
127 
128     foreach (DPluginAction* const ac, d->actions)
129     {
130         if (ac && (ac->parent() == parent))
131         {
132             list << ac;
133         }
134     }
135 
136     std::sort(list.begin(), list.end(), DPluginAction::pluginActionLessThan);
137     return list;
138 }
139 
findActionByName(const QString & name,QObject * const parent) const140 DPluginAction* DPluginGeneric::findActionByName(const QString& name, QObject* const parent) const
141 {
142     foreach (DPluginAction* const ac, actions(parent))
143     {
144         if (ac && (ac->objectName() == name))
145         {
146             return ac;
147         }
148     }
149 
150     return nullptr;
151 }
152 
addAction(DPluginAction * const ac)153 void DPluginGeneric::addAction(DPluginAction* const ac)
154 {
155     ac->setProperty("DPluginIId",      iid());
156     ac->setProperty("DPluginIfaceIId", ifaceIid());
157     d->actions.append(ac);
158 }
159 
categories() const160 QStringList DPluginGeneric::categories() const
161 {
162     QStringList list;
163 
164     foreach (DPluginAction* const ac, d->actions)
165     {
166         QString cat = ac->actionCategoryToString();
167 
168         if (!list.contains(cat))
169         {
170             list << cat;
171         }
172     }
173 
174     list.sort();
175 
176     return list;
177 }
178 
reactivateToolDialog(QWidget * const dlg) const179 bool DPluginGeneric::reactivateToolDialog(QWidget* const dlg) const
180 {
181     if (dlg && (dlg->isMinimized() || !dlg->isHidden()))
182     {
183         dlg->showNormal();       // krazy:exclude=qmethods
184         dlg->activateWindow();
185         dlg->raise();
186         return true;
187     }
188 
189     return false;
190 }
191 
192 } // namespace Digikam
193