summaryrefslogtreecommitdiff
path: root/CopyEngineManager.cpp
blob: 61a13997ea2feb1a26032b332865c7c9bae4ffd5 (plain)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/** \file CopyEngineManager.cpp
\brief Define the copy engine manager
\author alpha_one_x86
\licence GPL3, see the file COPYING */

#include <QMessageBox>

#include "CopyEngineManager.h"
#include "LanguagesManager.h"
#include "cpp11addition.h"

#ifdef ULTRACOPIER_PLUGIN_ALL_IN_ONE_DIRECT
#include "plugins/CopyEngine/Ultracopier-Spec/CopyEngineFactory.h"
#endif

CopyEngineManager::CopyEngineManager(OptionDialog *optionDialog)
{
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
    this->optionDialog=optionDialog;
    //setup the ui layout
    PluginsManager::pluginsManager->lockPluginListEdition();
    connect(this,&CopyEngineManager::previouslyPluginAdded,			this,&CopyEngineManager::onePluginAdded,Qt::QueuedConnection);
    connect(PluginsManager::pluginsManager,&PluginsManager::onePluginAdded,                this,&CopyEngineManager::onePluginAdded,Qt::QueuedConnection);
    #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE
    connect(PluginsManager::pluginsManager,&PluginsManager::onePluginWillBeRemoved,		this,&CopyEngineManager::onePluginWillBeRemoved,Qt::DirectConnection);
    #endif
    connect(PluginsManager::pluginsManager,&PluginsManager::pluginListingIsfinish,			this,&CopyEngineManager::allPluginIsloaded,Qt::QueuedConnection);
    std::vector<PluginsAvailable> list=PluginsManager::pluginsManager->getPluginsByCategory(PluginType_CopyEngine);
    foreach(PluginsAvailable currentPlugin,list)
        emit previouslyPluginAdded(currentPlugin);
    PluginsManager::pluginsManager->unlockPluginListEdition();
    //load the options
    isConnected=false;
}

void CopyEngineManager::onePluginAdded(const PluginsAvailable &plugin)
{
    if(plugin.category!=PluginType_CopyEngine)
        return;
    //setFileName
    std::string pluginPath=plugin.path+PluginsManager::getResolvedPluginName("copyEngine");
    #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE
    /*more IO
    if(!QFile(pluginPath).exists())
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"The plugin binary is missing: "+pluginPath);
        return;
    }*/
    #endif
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start: "+pluginPath);
    //search into loaded session
    unsigned int index=0;
    while(index<pluginList.size())
    {
        if(pluginList.at(index).pluginPath==pluginPath)
        {
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Critical,"Engine already found!");
            return;
        }
        index++;
    }
    CopyEnginePlugin newItem;
    newItem.pluginPath=pluginPath;
    newItem.path=plugin.path;
    newItem.name=plugin.name;
    #ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE_DIRECT
    newItem.pointer=new QPluginLoader(QString::fromStdString(newItem.pluginPath));
    #ifdef ULTRACOPIER_PLUGIN_ALL_IN_ONE
    QObjectList objectList=QPluginLoader::staticInstances();
    index=0;
    QObject *pluginObject;
    while(index<objectList.size())
    {
        pluginObject=objectList.at(index);
        newItem.factory = qobject_cast<PluginInterface_CopyEngineFactory *>(pluginObject);
        if(newItem.factory!=NULL)
            break;
        index++;
    }
    if(index==objectList.size())
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"static copy engine not found");
        return;
    }
    #else
    QObject *pluginObject = newItem.pointer->instance();
    if(pluginObject==NULL)
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"unable to load the plugin: "+newItem.pointer->errorString().toStdString());
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"unable to load the plugin for "+newItem.pluginPath);
        newItem.pointer->unload();
        return;
    }
    newItem.factory = qobject_cast<PluginInterface_CopyEngineFactory *>(pluginObject);
    //check if found
    index=0;
    while(index<pluginList.size())
    {
        if(pluginList.at(index).factory==newItem.factory)
        {
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Plugin already found");
            newItem.pointer->unload();
            return;
        }
        index++;
    }
    if(newItem.factory==NULL)
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"unable to cast the plugin: "+newItem.pointer->errorString().toStdString());
        newItem.pointer->unload();
        return;
    }
    #endif
    #else
    newItem.factory=new CopyEngineFactory();
    #endif

    #ifdef ULTRACOPIER_DEBUG
    connect(newItem.factory,&PluginInterface_CopyEngineFactory::debugInformation,this,&CopyEngineManager::debugInformation,Qt::QueuedConnection);
    #endif // ULTRACOPIER_DEBUG
    newItem.options=new LocalPluginOptions("CopyEngine-"+newItem.name);
    newItem.factory->setResources(newItem.options,plugin.writablePath,plugin.path,&FacilityEngine::facilityEngine,ULTRACOPIER_VERSION_PORTABLE_BOOL);
    connect(OptionEngine::optionEngine,&OptionEngine::resetOptions,newItem.factory,&PluginInterface_CopyEngineFactory::resetOptions);
    newItem.optionsWidget=newItem.factory->options();
    newItem.supportedProtocolsForTheSource=newItem.factory->supportedProtocolsForTheSource();
    newItem.supportedProtocolsForTheDestination=newItem.factory->supportedProtocolsForTheDestination();
    newItem.canDoOnlyCopy=newItem.factory->canDoOnlyCopy();
    newItem.type=newItem.factory->getCopyType();
    newItem.transferListOperation=newItem.factory->getTransferListOperation();
    optionDialog->addPluginOptionWidget(PluginType_CopyEngine,newItem.name,newItem.optionsWidget);
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"plugin: "+newItem.name+" loaded, send options");
    //emit newCopyEngineOptions(plugin.path,newItem.name,newItem.optionsWidget);
    pluginList.push_back(newItem);
    connect(LanguagesManager::languagesManager,&LanguagesManager::newLanguageLoaded,newItem.factory,&PluginInterface_CopyEngineFactory::newLanguageLoaded);
    if(PluginsManager::pluginsManager->allPluginHaveBeenLoaded())
        allPluginIsloaded();
    if(isConnected)
        emit addCopyEngine(newItem.name,newItem.canDoOnlyCopy);
}

#ifndef ULTRACOPIER_PLUGIN_ALL_IN_ONE
void CopyEngineManager::onePluginWillBeRemoved(const PluginsAvailable &plugin)
{
    unsigned int index=0;
    while(index<pluginList.size())
    {
        if(pluginList.at(index).path==plugin.path)
        {
            if(pluginList.at(index).intances.size()<=0)
            {
                emit removeCopyEngine(pluginList.at(index).name);
                pluginList.erase(pluginList.begin()+index);
                allPluginIsloaded();
                return;
            }
        }
        index++;
    }
}

void CopyEngineManager::onePluginWillBeUnloaded(const PluginsAvailable &plugin)
{
    if(plugin.category!=PluginType_CopyEngine)
        return;
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
    unsigned int index=0;
    while(index<pluginList.size())
    {
        if(pluginList.at(index).path==plugin.path)
        {
            delete pluginList.at(index).options;
            delete pluginList.at(index).factory;
            pluginList.at(index).pointer->unload();
            return;
        }
        index++;
    }
}
#endif

CopyEngineManager::returnCopyEngine CopyEngineManager::getCopyEngine(const Ultracopier::CopyMode &mode,
    const std::vector<std::string> &protocolsUsedForTheSources,const std::string &protocolsUsedForTheDestination)
{
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start, pluginList.size(): "+std::to_string(pluginList.size())+", mode: "+std::to_string((int)mode)+", and particular protocol");
    returnCopyEngine temp;
    unsigned int index=0;
    bool isTheGoodEngine=false;
    while(index<pluginList.size())
    {
        const CopyEnginePlugin &copyEnginePlugin=pluginList.at(index);
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"pluginList.at("+std::to_string(index)+").name: "+copyEnginePlugin.name);
        isTheGoodEngine=false;
        if(mode!=Ultracopier::Move || !copyEnginePlugin.canDoOnlyCopy)
        {
            if(protocolsUsedForTheSources.size()==0)
                isTheGoodEngine=true;
            else
            {
                ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"copyEnginePlugin.supportedProtocolsForTheDestination: "+stringimplode(copyEnginePlugin.supportedProtocolsForTheDestination,";"));
                ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"protocolsUsedForTheDestination: "+protocolsUsedForTheDestination);
                if(protocolsUsedForTheDestination.empty() || vectorcontainsAtLeastOne(copyEnginePlugin.supportedProtocolsForTheDestination,protocolsUsedForTheDestination))
                {
                    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"copyEnginePlugin.supportedProtocolsForTheSource: "+stringimplode(copyEnginePlugin.supportedProtocolsForTheSource,";"));
                    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"protocolsUsedForTheSources.at(indexProto): "+stringimplode(protocolsUsedForTheSources,";"));
                    isTheGoodEngine=true;
                    unsigned int indexProto=0;
                    while(indexProto<protocolsUsedForTheSources.size())
                    {
                        if(!vectorcontainsAtLeastOne(copyEnginePlugin.supportedProtocolsForTheSource,protocolsUsedForTheSources.at(indexProto)))
                        {
                            isTheGoodEngine=false;
                            break;
                        }
                        indexProto++;
                    }
                }
            }
        }
        if(isTheGoodEngine)
        {
            pluginList[index].intances.push_back(pluginList.at(index).factory->getInstance());
            temp.engine=pluginList.at(index).intances.back();
            temp.canDoOnlyCopy=pluginList.at(index).canDoOnlyCopy;
            temp.havePause=pluginList.at(index).factory->havePause();
            temp.type=pluginList.at(index).type;
            temp.transferListOperation=pluginList.at(index).transferListOperation;
            return temp;
        }
        index++;
    }
    if(mode==Ultracopier::Move)
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Cannot find any copy engine with move support");
        QMessageBox::critical(NULL,tr("Warning"),tr("Cannot find any copy engine with move support"));
    }
    else
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Cannot find any compatible engine!");
        QMessageBox::critical(NULL,tr("Warning"),tr("Cannot find any compatible engine!"));
    }
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"protocolsUsedForTheSources: "+stringimplode(protocolsUsedForTheSources,";")+", protocolsUsedForTheDestination: "+protocolsUsedForTheDestination);

    temp.engine=NULL;
    temp.type=Ultracopier::File;
    temp.canDoOnlyCopy=true;
    return temp;
}

CopyEngineManager::returnCopyEngine CopyEngineManager::getCopyEngine(const Ultracopier::CopyMode &mode,const std::string &name)
{
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start, pluginList.size(): "+std::to_string(pluginList.size())+", with mode: "+std::to_string((int)mode)+", and name: "+name);
    returnCopyEngine temp;
    unsigned int index=0;
    while(index<pluginList.size())
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"Check matching: "+pluginList.at(index).name);
        if(pluginList.at(index).name==name || name.empty())
        {
            if(mode==Ultracopier::Move && pluginList.at(index).canDoOnlyCopy)
            {
                ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"This copy engine does not support move: pluginList.at(index).canDoOnlyCopy: "+std::to_string(pluginList.at(index).canDoOnlyCopy));
                QMessageBox::critical(NULL,tr("Warning"),tr("This copy engine does not support move"));
                temp.engine=NULL;
                return temp;
            }
            PluginInterface_CopyEngineFactory * factory=pluginList.at(index).factory;
            PluginInterface_CopyEngine * newIntance=factory->getInstance();
            pluginList[index].intances.push_back(newIntance);
            temp.engine=pluginList.at(index).intances.back();
            temp.canDoOnlyCopy=pluginList.at(index).canDoOnlyCopy;
            temp.type=pluginList.at(index).type;
            temp.havePause=factory->havePause();
            temp.transferListOperation=pluginList.at(index).transferListOperation;
            return temp;
        }
        index++;
    }
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Cannot find any engine with this name: "+name);
    QMessageBox::critical(NULL,tr("Warning"),tr("Cannot find any engine with this name: %1").arg(QString::fromStdString(name)));
    temp.engine=NULL;
    temp.type=Ultracopier::File;
    temp.canDoOnlyCopy=true;
    return temp;
}

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

/// \brief To notify when new value into a group have changed
void CopyEngineManager::newOptionValue(const std::string &groupName,const std::string &variableName,const std::string &value)
{
    if(groupName=="CopyEngine" && variableName=="List")
    {
        Q_UNUSED(value)
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Information,"start(\""+groupName+"\",\""+variableName+"\",\""+value+"\")");
        allPluginIsloaded();
    }
}

void CopyEngineManager::setIsConnected()
{
    /* ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
      prevent bug, I don't know why, in one case it bug here
      */
    isConnected=true;
    unsigned int index=0;
    while(index<pluginList.size())
    {
        emit addCopyEngine(pluginList.at(index).name,pluginList.at(index).canDoOnlyCopy);
        index++;
    }
}

void CopyEngineManager::allPluginIsloaded()
{
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start");
    std::vector<std::string> actualList;
    unsigned int index=0;
    while(index<pluginList.size())
    {
        actualList.push_back(pluginList.at(index).name);
        index++;
    }
    std::vector<std::string> preferedList=stringsplit(OptionEngine::optionEngine->getOptionValue("CopyEngine","List"),';');
    vectorRemoveDuplicatesForSmallList(preferedList);
    vectorRemoveDuplicatesForSmallList(actualList);
    index=0;
    while(index<preferedList.size())
    {
        if(!vectorcontainsAtLeastOne(actualList,preferedList.at(index)))
        {
            preferedList.erase(preferedList.cbegin()+index);
            index--;
        }
        index++;
    }
    index=0;
    while(index<actualList.size())
    {
        if(!vectorcontainsAtLeastOne(preferedList,actualList.at(index)))
            preferedList.push_back( actualList.at(index));
        index++;
    }
    OptionEngine::optionEngine->setOptionValue("CopyEngine","List",stringimplode(preferedList,';'));
    std::vector<CopyEnginePlugin> newPluginList;
    index=0;
    while(index<preferedList.size())
    {
        unsigned int pluginListIndex=0;
        while(pluginListIndex<pluginList.size())
        {
            if(preferedList.at(index)==pluginList.at(pluginListIndex).name)
            {
                newPluginList.push_back(pluginList.at(pluginListIndex));
                break;
            }
            pluginListIndex++;
        }
        index++;
    }
    pluginList=newPluginList;
}

bool CopyEngineManager::protocolsSupportedByTheCopyEngine(PluginInterface_CopyEngine * engine,const std::vector<std::string> &protocolsUsedForTheSources,const std::string &protocolsUsedForTheDestination)
{
    unsigned int index=0;
    while(index<pluginList.size())
    {
        const CopyEnginePlugin &copyEnginePlugin=pluginList.at(index);
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"pluginList.at("+std::to_string(index)+").name: "+copyEnginePlugin.name);
        if(vectorcontainsAtLeastOne(copyEnginePlugin.intances,engine))
        {
            if(!vectorcontainsAtLeastOne(copyEnginePlugin.supportedProtocolsForTheDestination,protocolsUsedForTheDestination))
                return false;
            unsigned int indexProto=0;
            while(indexProto<protocolsUsedForTheSources.size())
            {
                if(!vectorcontainsAtLeastOne(copyEnginePlugin.supportedProtocolsForTheSource,protocolsUsedForTheSources.at(indexProto)))
                    return false;
                indexProto++;
            }
            return true;
        }
    }
    return false;
}