File: PluginLoaderCore.cpp

package info (click to toggle)
ultracopier 2.2.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 18,548 kB
  • sloc: cpp: 57,995; ansic: 8,128; sh: 2,520; xml: 677; makefile: 26
file content (339 lines) | stat: -rwxr-xr-x 12,868 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/** \file PluginLoader.h
\brief Define the plugin loader
\author alpha_one_x86
\licence GPL3, see the file COPYING */

#include "PluginLoaderCore.h"
#include "LanguagesManager.h"

#ifdef ULTRACOPIER_PLUGIN_ALL_IN_ONE
#ifdef Q_OS_WIN32
#include "plugins/PluginLoader/catchcopy-v0002/pluginLoader.h"
#endif
#endif

PluginLoaderCore::PluginLoaderCore(OptionDialog *optionDialog)
{
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
    this->optionDialog=optionDialog;
    //load the overall instance
    //load the plugin
    PluginsManager::pluginsManager->lockPluginListEdition();
    connect(this,&PluginLoaderCore::previouslyPluginAdded,	this,&PluginLoaderCore::onePluginAdded,Qt::QueuedConnection);
    connect(PluginsManager::pluginsManager,&PluginsManager::onePluginAdded,	this,&PluginLoaderCore::onePluginAdded,Qt::QueuedConnection);
    #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE
    connect(PluginsManager::pluginsManager,&PluginsManager::onePluginWillBeRemoved,this,&PluginLoaderCore::onePluginWillBeRemoved,Qt::DirectConnection);
    #endif
    connect(PluginsManager::pluginsManager,&PluginsManager::pluginListingIsfinish,	this,&PluginLoaderCore::allPluginIsloaded,Qt::QueuedConnection);
    std::vector<PluginsAvailable> list=PluginsManager::pluginsManager->getPluginsByCategory(PluginType_PluginLoader);
    foreach(PluginsAvailable currentPlugin,list)
        emit previouslyPluginAdded(currentPlugin);
    PluginsManager::pluginsManager->unlockPluginListEdition();
    needEnable=false;
    last_state=Ultracopier::Uncaught;
    last_have_plugin=false;
    last_inWaitOfReply=false;
    stopIt=false;
}

PluginLoaderCore::~PluginLoaderCore()
{
    stopIt=true;
    #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE_DIRECT
    {
        /* why it crash here? Only under Window with PluginLoader/catchcopy-v0002
        int index=0;
        while(index<pluginList.size())
        {
            pluginList.at(index).pluginLoaderInterface->setEnabled(false);
            if(pluginList.at(index).pluginLoader!=NULL)
            {
                if(!pluginList.at(index).pluginLoader->isLoaded() || pluginList.at(index).pluginLoader->unload())
                {
                    delete pluginList.at(index).options;
                    pluginList.removeAt(index);
                }
                else
                    index++;
            }
            else
                index++;
        }//*/
    }
    #endif
}

void PluginLoaderCore::resendState()
{
    if(stopIt)
        return;
    sendState(true);
}

void PluginLoaderCore::onePluginAdded(const PluginsAvailable &plugin)
{
    #ifdef ULTRACOPIER_PLUGIN_ALL_IN_ONE
    #ifdef Q_OS_WIN32
    PluginInterface_PluginLoader *factory;
    #endif
    #endif
    #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE_DIRECT
    if(stopIt)
        return;
    if(plugin.category!=PluginType_PluginLoader)
        return;
    LocalPlugin newEntry;
    std::string pluginPath=plugin.path+PluginsManager::getResolvedPluginName("pluginLoader");
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"try load: "+pluginPath);
    #ifdef ULTRACOPIER_PLUGIN_ALL_IN_ONE
    PluginInterface_PluginLoader *pluginLoaderInstance;
    QObjectList objectList=QPluginLoader::staticInstances();
    int index=0;
    QObject *pluginObject;
    while(index<objectList.size())
    {
        pluginObject=objectList.at(index);
        pluginLoaderInstance = qobject_cast<PluginInterface_PluginLoader *>(pluginObject);
        if(pluginLoaderInstance!=NULL)
            break;
        index++;
    }
    if(index==objectList.size())
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"static listener not found");
        return;
    }
    newEntry.pluginLoader=NULL;
    #else
    QPluginLoader *pluginLoader= new QPluginLoader(QString::fromStdString(pluginPath));
    QObject *pluginInstance = pluginLoader->instance();
    if(!pluginInstance)
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"unable to load the plugin: "+pluginLoader->errorString().toStdString());
        return;
    }
    PluginInterface_PluginLoader *pluginLoaderInstance = qobject_cast<PluginInterface_PluginLoader *>(pluginInstance);
    if(!pluginLoaderInstance)
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"unable to cast the plugin: "+pluginLoader->errorString().toStdString());
        return;
    }
    newEntry.pluginLoader			= pluginLoader;
    //check if found
    unsigned int index=0;
    while(index<pluginList.size())
    {
        if(pluginList.at(index).pluginLoaderInterface==pluginLoaderInstance)
        {
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Plugin already found");
            pluginLoader->unload();
            return;
        }
        index++;
    }
    #endif
    #ifdef ULTRACOPIER_DEBUG
    connect(pluginLoaderInstance,&PluginInterface_PluginLoader::debugInformation,this,&PluginLoaderCore::debugInformation,Qt::DirectConnection);
    #endif // ULTRACOPIER_DEBUG

    newEntry.options=new LocalPluginOptions("PluginLoader-"+plugin.name);
    newEntry.pluginLoaderInterface		= pluginLoaderInstance;
    newEntry.path				= plugin.path;
    newEntry.state				= Ultracopier::Uncaught;
    newEntry.inWaitOfReply			= false;
    pluginList.push_back(newEntry);
    pluginLoaderInstance->setResources(newEntry.options,plugin.writablePath,plugin.path,ULTRACOPIER_VERSION_PORTABLE_BOOL);
    optionDialog->addPluginOptionWidget(PluginType_PluginLoader,plugin.name,newEntry.pluginLoaderInterface->options());
    connect(pluginList.back().pluginLoaderInterface,&PluginInterface_PluginLoader::newState,this,&PluginLoaderCore::newState,Qt::DirectConnection);
    connect(LanguagesManager::languagesManager,&LanguagesManager::newLanguageLoaded,newEntry.pluginLoaderInterface,&PluginInterface_PluginLoader::newLanguageLoaded,Qt::DirectConnection);
    if(needEnable)
    {
        pluginList.back().inWaitOfReply=true;
        newEntry.pluginLoaderInterface->setEnabled(needEnable);
    }
    #else
    #ifdef Q_OS_WIN32
    factory=new WindowsExplorerLoader();
    LocalPlugin newEntry;
    #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE_DIRECT
    newEntry.pluginLoader=NULL;
    #endif

    newEntry.options=new LocalPluginOptions("PluginLoader-"+plugin.name);
    newEntry.pluginLoaderInterface		= new WindowsExplorerLoader();
    newEntry.path				= plugin.path;
    newEntry.state				= Ultracopier::Uncaught;
    newEntry.inWaitOfReply			= false;
    #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE_DIRECT
    #ifdef ULTRACOPIER_DEBUG
    connect(newEntry.pluginLoaderInterface,&PluginInterface_PluginLoader::debugInformation,this,&PluginLoaderCore::debugInformation,Qt::DirectConnection);
    #endif // ULTRACOPIER_DEBUG
    #endif
    pluginList.push_back(newEntry);
    newEntry.pluginLoaderInterface->setResources(newEntry.options,plugin.writablePath,plugin.path,ULTRACOPIER_VERSION_PORTABLE_BOOL);
    optionDialog->addPluginOptionWidget(PluginType_PluginLoader,plugin.name,newEntry.pluginLoaderInterface->options());
    connect(pluginList.back().pluginLoaderInterface,&PluginInterface_PluginLoader::newState,this,&PluginLoaderCore::newState,Qt::DirectConnection);
    connect(LanguagesManager::languagesManager,&LanguagesManager::newLanguageLoaded,newEntry.pluginLoaderInterface,&PluginInterface_PluginLoader::newLanguageLoaded,Qt::DirectConnection);
    if(needEnable)
    {
        pluginList.back().inWaitOfReply=true;
        newEntry.pluginLoaderInterface->setEnabled(needEnable);
    }
    #endif
    Q_UNUSED(plugin);
    #endif
}

#ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE
void PluginLoaderCore::onePluginWillBeRemoved(const PluginsAvailable &plugin)
{
    if(stopIt)
        return;
    if(plugin.category!=PluginType_PluginLoader)
        return;
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
    unsigned int index=0;
    while(index<pluginList.size())
    {
        if(plugin.path==pluginList.at(index).path)
        {
            pluginList.at(index).pluginLoaderInterface->setEnabled(false);
            if(pluginList.at(index).pluginLoader!=NULL)
            {
                if(!pluginList.at(index).pluginLoader->isLoaded() || pluginList.at(index).pluginLoader->unload())
                {
                    delete pluginList.at(index).options;
                    pluginList.erase(pluginList.cbegin()+index);
                }
            }
            sendState();
            return;
        }
        index++;
    }
}
#endif

void PluginLoaderCore::load()
{
    if(stopIt)
        return;
    needEnable=true;
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
    unsigned int index=0;
    while(index<pluginList.size())
    {
        pluginList[index].inWaitOfReply=true;
        pluginList.at(index).pluginLoaderInterface->setEnabled(true);
        index++;
    }
    sendState(true);
}

void PluginLoaderCore::unload()
{
    if(stopIt)
        return;
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
    needEnable=false;
    unsigned int index=0;
    while(index<pluginList.size())
    {
        pluginList[index].inWaitOfReply=true;
        pluginList.at(index).pluginLoaderInterface->setEnabled(false);
        index++;
    }
    sendState(true);
}

#ifdef ULTRACOPIER_DEBUG
void PluginLoaderCore::debugInformation(const Ultracopier::DebugLevel &level,const std::string& fonction,const std::string& text,const std::string& file,const unsigned int& ligne)
{
    DebugEngine::addDebugInformationStatic(level,fonction,text,file,ligne,"Plugin loader plugin");
}
#endif // ULTRACOPIER_DEBUG

void PluginLoaderCore::allPluginIsloaded()
{
    if(stopIt)
        return;
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"with value: "+std::to_string(pluginList.size()>0));
    sendState(true);
}

void PluginLoaderCore::sendState(bool force)
{
    if(stopIt)
        return;
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start, pluginList.size(): "+std::to_string(pluginList.size())+", force: "+std::to_string(force));
    Ultracopier::CatchState current_state=Ultracopier::Uncaught;
    bool found_not_listen=false,found_listen=false,found_inWaitOfReply=false;
    unsigned int index=0;
    while(index<pluginList.size())
    {
        if(current_state==Ultracopier::Uncaught)
        {
            if(pluginList.at(index).state==Ultracopier::Semiuncaught)
                current_state=Ultracopier::Semiuncaught;
            else if(pluginList.at(index).state==Ultracopier::Uncaught)
                found_not_listen=true;
            else if(pluginList.at(index).state==Ultracopier::Caught)
                found_listen=true;
        }
        if(pluginList.at(index).inWaitOfReply)
            found_inWaitOfReply=true;
        index++;
    }
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"current_state: "+std::to_string(current_state));
    if(current_state==Ultracopier::Uncaught)
    {
        if(!found_not_listen && !found_listen)
        {
            if(needEnable)
                current_state=Ultracopier::Caught;
        }
        else if(found_not_listen && !found_listen)
            current_state=Ultracopier::Uncaught;
        else if(!found_not_listen && found_listen)
            current_state=Ultracopier::Caught;
        else
            current_state=Ultracopier::Semiuncaught;
    }
    bool have_plugin=pluginList.size()>0;
    if(force || current_state!=last_state || have_plugin!=last_have_plugin || found_inWaitOfReply!=last_inWaitOfReply)
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"send pluginLoaderReady("+std::to_string(current_state)+","+std::to_string(have_plugin)+","+std::to_string(found_inWaitOfReply)+")");
        emit pluginLoaderReady(current_state,have_plugin,found_inWaitOfReply);
    }
    else
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"Skip the signal sending");
    last_state=current_state;
    last_have_plugin=have_plugin;
    last_inWaitOfReply=found_inWaitOfReply;
}

void PluginLoaderCore::newState(const Ultracopier::CatchState &state)
{
    if(stopIt)
        return;
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start, state: "+std::to_string(state));
    PluginInterface_PluginLoader *temp=qobject_cast<PluginInterface_PluginLoader *>(QObject::sender());
    if(temp==NULL)
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"listener not located!");
        return;
    }
    unsigned int index=0;
    while(index<pluginList.size())
    {
        if(temp==pluginList.at(index).pluginLoaderInterface)
        {
            pluginList[index].state=state;
            pluginList[index].inWaitOfReply=false;
            sendState(true);
            return;
        }
        index++;
    }
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"listener not found!");
}