summaryrefslogtreecommitdiff
path: root/little/main-little.cpp
blob: ca7de1ac8ecb037ebc7be641ab4af9e767de4c67 (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
/** \file main.cpp
\brief Define the main() for the point entry
\author alpha_one_x86
\licence GPL3, see the file COPYING */

#include <QApplication>
#include <QtPlugin>
#include "../Variable.h"
#include "../plugins/CopyEngine/Ultracopier/CopyEngineFactory.h"
#include "../plugins/Themes/Oxygen/ThemesFactory.h"
#include "OptionsEngineLittle.h"
#include "../FacilityEngine.h"
#include <iostream>

Themes * interface=NULL;
CopyEngine * engine=NULL;
uint64_t lastProgression=0;
uint64_t currentProgression=0;
uint64_t totalProgression=0;//store the file byte transfered, used into the remaining time
QTime lastProgressionTime;
QTimer forUpateInformation;///< used to call \see periodicSynchronization()

/** for RemainingTimeAlgo_Traditional **/
//this speed is for instant speed
std::vector<uint64_t> lastSpeedDetected;//stored in bytes
std::vector<double> lastSpeedTime;//stored in ms
//this speed is average speed on more time to calculate the remaining time
std::vector<uint64_t> lastAverageSpeedDetected;//stored in bytes
std::vector<double> lastAverageSpeedTime;//stored in ms

struct RemainingTimeLogarithmicColumn
{
    std::vector<int> lastProgressionSpeed;
    uint64_t totalSize;
    uint64_t transferedSize;
};
/** for RemainingTimeAlgo_Logarithmic **/
std::vector<RemainingTimeLogarithmicColumn> remainingTimeLogarithmicValue;

void connectEngine();
void connectInterfaceAndSync();
void periodicSynchronization();

/// \brief Define the main() for the point entry
int main(int argc, char *argv[])
{
    QApplication ultracopierApplication(argc, argv);
    ultracopierApplication.setApplicationVersion(ULTRACOPIER_VERSION);
    qRegisterMetaType<Ultracopier::CopyMode>("Ultracopier::CopyMode");
    qRegisterMetaType<Ultracopier::ItemOfCopyList>("Ultracopier::ItemOfCopyList");
    qRegisterMetaType<std::string>("std::string");
    qRegisterMetaType<std::vector<std::string> >("std::vector<std::string>");
    qRegisterMetaType<Ultracopier::DebugLevel>("Ultracopier::DebugLevel");
    qRegisterMetaType<Ultracopier::EngineActionInProgress>("Ultracopier::EngineActionInProgress");
    qRegisterMetaType<std::vector<Ultracopier::ReturnActionOnCopyList> >("std::vector<Ultracopier::ReturnActionOnCopyList>");
    qRegisterMetaType<uint64_t>("uint64_t");
    qRegisterMetaType<std::vector<Ultracopier::ProgressionItem> >("std::vector<Ultracopier::ProgressionItem>");

    FacilityEngine facilityEngine;
    ThemesFactory themesFactory;
    themesFactory.setResources(new OptionsEngineLittle(),std::string(),std::string(),&facilityEngine,true);
    CopyEngineFactory copyEngineFactory;
    copyEngineFactory.setResources(new OptionsEngineLittle(),std::string(),std::string(),&facilityEngine,true);

    interface=static_cast<Themes *>(themesFactory.getInstance());
    engine=static_cast<CopyEngine *>(copyEngineFactory.getInstance());

    connectEngine();
    connectInterfaceAndSync();
    forUpateInformation.setInterval(ULTRACOPIER_TIME_INTERFACE_UPDATE);
    forUpateInformation.start();
    QObject::connect(&forUpateInformation,	&QTimer::timeout,						&periodicSynchronization);

    const int returnVar=ultracopierApplication.exec();

    delete engine;
    return returnVar;
}

void connectEngine()
{
    bool failed=false;
    failed|=!QObject::connect(engine,&CopyEngine::newFolderListing,			interface,&Themes::newFolderListing,Qt::QueuedConnection);//to check to change
    failed|=!QObject::connect(engine,&CopyEngine::actionInProgess,           interface,&Themes::actionInProgess,Qt::QueuedConnection);
    failed|=!QObject::connect(engine,&CopyEngine::isInPause,                 interface,&Themes::isInPause,Qt::QueuedConnection);//to check to change
    failed|=!QObject::connect(engine,&CopyEngine::cancelAll,					QCoreApplication::instance(),&QCoreApplication::quit,Qt::QueuedConnection);
    failed|=!QObject::connect(engine,&CopyEngine::canBeDeleted,					QCoreApplication::instance(),&QCoreApplication::quit,Qt::QueuedConnection);
    /*failed|=!QObject::connect(engine,&CopyEngine::error,                     interface,&Themes::error,Qt::QueuedConnection);
    failed|=!QObject::connect(engine,&CopyEngine::rmPath,                    interface,&Themes::rmPath,Qt::QueuedConnection);
    failed|=!QObject::connect(engine,&CopyEngine::mkPath,                    interface,&Themes::mkPath,Qt::QueuedConnection);
    failed|=!QObject::connect(engine,&CopyEngine::syncReady,					interface,&Themes::syncReady,Qt::QueuedConnection);
    failed|=!QObject::connect(engine,&CopyEngine::doneTime,					interface,&Themes::doneTime,Qt::QueuedConnection);*/
    if(failed)
    {
        std::cerr << "Little version, connectEngine() failed, abort" << std::endl;
        abort();
    }
}

void connectInterfaceAndSync()
{
    bool failed=false;
    failed|=!QObject::connect(interface,&Themes::pause,                      engine,&CopyEngine::pause);
    failed|=!QObject::connect(interface,&Themes::resume,                     engine,&CopyEngine::resume);
    failed|=!QObject::connect(interface,&Themes::skip,                       engine,&CopyEngine::skip);
    failed|=!QObject::connect(interface,&Themes::newSpeedLimitation,         engine,&CopyEngine::setSpeedLimitation);
    failed|=!QObject::connect(interface,&Themes::userAddFolder,              engine,&CopyEngine::userAddFolder);
    failed|=!QObject::connect(interface,&Themes::userAddFile,                engine,&CopyEngine::userAddFile);
    failed|=!QObject::connect(interface,&Themes::removeItems,                engine,&CopyEngine::removeItems);
    failed|=!QObject::connect(interface,&Themes::moveItemsOnTop,             engine,&CopyEngine::moveItemsOnTop);
    failed|=!QObject::connect(interface,&Themes::moveItemsUp,                engine,&CopyEngine::moveItemsUp);
    failed|=!QObject::connect(interface,&Themes::moveItemsDown,              engine,&CopyEngine::moveItemsDown);
    failed|=!QObject::connect(interface,&Themes::moveItemsOnBottom,          engine,&CopyEngine::moveItemsOnBottom);
    failed|=!QObject::connect(interface,&Themes::exportTransferList,			engine,&CopyEngine::exportTransferList);
    //failed|=!QObject::connect(interface,&Themes::exportErrorIntoTransferList,engine,&CopyEngine::exportErrorIntoTransferList);
    failed|=!QObject::connect(interface,&Themes::importTransferList,			engine,&CopyEngine::importTransferList);

    /*failed|=!QObject::connect(interface,&Themes::newSpeedLimitation,         engine,&CopyEngine::resetSpeedDetectedInterface);
    failed|=!QObject::connect(interface,&Themes::resume,                     engine,&CopyEngine::resetSpeedDetectedInterface);
    failed|=!QObject::connect(interface,&Themes::urlDropped,                 engine,&CopyEngine::urlDropped,Qt::QueuedConnection);*/
    failed|=!QObject::connect(interface,&Themes::cancel,                     engine,&CopyEngine::cancel,Qt::QueuedConnection);
    failed|=!QObject::connect(engine,&CopyEngine::newActionOnList,           engine,&CopyEngine::newActionOnList,	Qt::QueuedConnection);

    failed|=!QObject::connect(engine,&CopyEngine::pushFileProgression,		interface,&Themes::setFileProgression,		Qt::QueuedConnection);
    failed|=!QObject::connect(engine,&CopyEngine::pushGeneralProgression,	interface,&Themes::setGeneralProgression,		Qt::QueuedConnection);
    failed|=!QObject::connect(engine,&CopyEngine::pushGeneralProgression,    engine,&CopyEngine::pushGeneralProgression,		Qt::QueuedConnection);
    failed|=!QObject::connect(engine,&CopyEngine::errorToRetry,              interface,&Themes::errorToRetry,		Qt::QueuedConnection);

    if(failed)
    {
        std::cerr << "Little version, connectEngine() failed, abort" << std::endl;
        abort();
    }
    interface->setSupportSpeedLimitation(engine->supportSpeedLimitation());
    interface->setCopyType(Ultracopier::CopyType::FileAndFolder);
    interface->setTransferListOperation(Ultracopier::TransferListOperation::TransferListOperation_None);
    interface->actionInProgess(Ultracopier::EngineActionInProgress::Idle);
    //interface->isInPause(currentCopyInstance.isPaused);
    interface->isInPause(false);

    interface->setSupportSpeedLimitation(engine->supportSpeedLimitation());
    QWidget *tempWidget=interface->getOptionsEngineWidget();
    if(tempWidget!=NULL)
        interface->getOptionsEngineEnabled(engine->getOptionsEngine(tempWidget));
    //important, to have the modal dialog
    engine->setInterfacePointer(interface);

    //put entry into the interface
    engine->syncTransferList();

    //force the updating, without wait the timer
    periodicSynchronization();
}

void periodicSynchronization()
{
    /** ***************** Do time calcul ******************* **/
    //if(!isPaused)
    {
        //calcul the last difference of the transfere
        uint64_t realByteTransfered=engine->realByteTransfered();
        uint64_t diffCopiedSize=0;
        if(realByteTransfered>=lastProgression)
            diffCopiedSize=realByteTransfered-lastProgression;
        lastProgression=realByteTransfered;

        // algo 1:
        // ((double)currentProgression)/totalProgression -> example: done 80% -> 0.8
        // baseTime+runningTime -> example: done into 80s, remaining time: 80/0.8-80=80*(1/0.8-1)=20s
        // algo 2 (not used):
        // remaining byte/current speed

        //remaining time: (total byte - lastProgression)/byte per ms since the start
        /*if(totalProgression==0 || currentProgression==0)
            interface->remainingTime(-1);
        else if((totalProgression-currentProgression)>1024)
            interface->remainingTime(transferAddedTime*((double)totalProgression/(double)currentProgression-1)/1000);*/

        //do the speed calculation
        if(lastProgressionTime.isNull())
            lastProgressionTime.start();
        else
        {
            //if((action==Ultracopier::Copying || action==Ultracopier::CopyingAndListing))
            {
                lastSpeedTime.push_back(lastProgressionTime.elapsed());
                lastSpeedDetected.push_back(diffCopiedSize);
                lastAverageSpeedTime.push_back(lastProgressionTime.elapsed());
                lastAverageSpeedDetected.push_back(diffCopiedSize);
                while(lastSpeedTime.size()>ULTRACOPIER_MAXVALUESPEEDSTORED)
                    lastSpeedTime.erase(lastSpeedTime.cbegin());
                while(lastSpeedDetected.size()>ULTRACOPIER_MAXVALUESPEEDSTORED)
                    lastSpeedDetected.erase(lastSpeedDetected.cbegin());
                while(lastAverageSpeedTime.size()>ULTRACOPIER_MAXVALUESPEEDSTOREDTOREMAININGTIME)
                    lastAverageSpeedTime.erase(lastAverageSpeedTime.cbegin());
                while(lastAverageSpeedDetected.size()>ULTRACOPIER_MAXVALUESPEEDSTOREDTOREMAININGTIME)
                    lastAverageSpeedDetected.erase(lastAverageSpeedDetected.cbegin());
                double totTime=0,totAverageTime=0;
                double totSpeed=0,totAverageSpeed=0;

                //current speed
                unsigned int index_sub_loop=0;
                while(index_sub_loop<lastSpeedDetected.size())
                {
                    totTime+=lastSpeedTime.at(index_sub_loop);
                    totSpeed+=lastSpeedDetected.at(index_sub_loop);
                    index_sub_loop++;
                }
                totTime/=1000;

                //speed to calculate the remaining time
                index_sub_loop=0;
                while(index_sub_loop<lastAverageSpeedDetected.size())
                {
                    totAverageTime+=lastAverageSpeedTime.at(index_sub_loop);
                    totAverageSpeed+=lastAverageSpeedDetected.at(index_sub_loop);
                    index_sub_loop++;
                }
                totAverageTime/=1000;

                if(totTime>0)
                    if(lastAverageSpeedDetected.size()>=ULTRACOPIER_MINVALUESPEED)
                        interface->detectedSpeed(totSpeed/totTime);

                if(totAverageTime>0)
                    if(lastAverageSpeedDetected.size()>=ULTRACOPIER_MINVALUESPEEDTOREMAININGTIME)
                    {
                        /*if(remainingTimeAlgo==Ultracopier::RemainingTimeAlgo_Traditional)
                        {
                            if(totSpeed>0)
                            {
                                //remaining time: (total byte - lastProgression)/byte per ms since the start
                                if(totalProgression==0 || currentProgression==0)
                                    interface->remainingTime(-1);
                                else if((totalProgression-currentProgression)>1024)
                                    interface->remainingTime((totalProgression-currentProgression)/(totAverageSpeed/totAverageTime));
                            }
                            else
                                interface->remainingTime(-1);
                        }
                        else if(remainingTimeAlgo==Ultracopier::RemainingTimeAlgo_Logarithmic)*/
                        {
                            int remainingTimeValue=0;
                            //calculate for each file class
                            index_sub_loop=0;
                            while(index_sub_loop<remainingTimeLogarithmicValue.size())
                            {
                                const RemainingTimeLogarithmicColumn &remainingTimeLogarithmicColumn=remainingTimeLogarithmicValue.at(index_sub_loop);
                                //normal detect
                                const quint64 &remainingSize=remainingTimeLogarithmicColumn.totalSize-remainingTimeLogarithmicColumn.transferedSize;
                                if(remainingTimeLogarithmicColumn.lastProgressionSpeed.size()>=ULTRACOPIER_MINVALUESPEED)
                                {
                                    int average_speed=0;
                                    unsigned int temp_loop_index=0;
                                    while(temp_loop_index<remainingTimeLogarithmicColumn.lastProgressionSpeed.size())
                                    {
                                        average_speed+=remainingTimeLogarithmicColumn.lastProgressionSpeed.at(temp_loop_index);
                                        temp_loop_index++;
                                    }
                                    average_speed/=remainingTimeLogarithmicColumn.lastProgressionSpeed.size();
                                    remainingTimeValue+=remainingSize/average_speed;
                                }
                                //fallback
                                else
                                {
                                    if(totSpeed>0)
                                    {
                                        //remaining time: (total byte - lastProgression)/byte per ms since the start
                                        if(totalProgression==0 || currentProgression==0)
                                            remainingTimeValue+=1;
                                        else if((totalProgression-currentProgression)>1024)
                                            remainingTimeValue+=remainingSize/totAverageSpeed;
                                    }
                                    else
                                        remainingTimeValue+=1;
                                }
                                index_sub_loop++;
                            }
                            interface->remainingTime(remainingTimeValue);
                        }
                        /*else
                        {}//error case*/
                    }
            }
            lastProgressionTime.restart();
        }
    }
}