summaryrefslogtreecommitdiffstats
path: root/src/libs/kdtools/kdupdaterupdateoperations.cpp
blob: 51fcaf7b13bcfda715aff3d11ac8915202c54da3 (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
/****************************************************************************
** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB.  All rights reserved.
**
** This file is part of the KD Tools library.
**
** Licensees holding valid commercial KD Tools licenses may use this file in
** accordance with the KD Tools Commercial License Agreement provided with
** the Software.
**
**
** This file may be distributed and/or modified under the terms of the
** GNU Lesser General Public License version 2 and version 3 as published by the
** Free Software Foundation and appearing in the file LICENSE.LGPL included.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** Contact info@kdab.com if any conditions of this licensing are not
** clear to you.
**
**********************************************************************/

#include "kdupdaterupdateoperations.h"
#include "kdupdaterapplication.h"
#include "kdupdaterpackagesinfo.h"
#include "environment.h"

#include <QFile>
#include <QDir>
#include <QDirIterator>
#include <QProcess>
#include <QTextStream>
#include <QDebug>
#include <QTemporaryFile>


#include <cerrno>

#define SUPPORT_DETACHED_PROCESS_EXECUTION

#ifdef SUPPORT_DETACHED_PROCESS_EXECUTION
#ifdef Q_OS_WIN
#include <windows.h>
#endif
#endif

using namespace KDUpdater;

static QString errnoToQString(int error)
{
#if defined(Q_OS_WIN) && !defined(Q_CC_MINGW)
    char msg[128];
    if (strerror_s(msg, sizeof msg, error) != 0)
        return QString::fromLocal8Bit(msg);
    return QString();
#else
    return QString::fromLocal8Bit(strerror(error));
#endif
}

static bool removeDirectory(const QString &path, QString *errorString)
{
    Q_ASSERT(errorString);
    const QFileInfoList entries = QDir(path).entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden);
    for (QFileInfoList::const_iterator it = entries.constBegin(); it != entries.constEnd(); ++it) {
        if (it->isDir() && !it->isSymLink()) {
            removeDirectory(it->filePath(), errorString);
        } else {
            QFile f(it->filePath());
            if (!f.remove())
                return false;
        }
    }

    errno = 0;
    const bool success = QDir().rmdir(path);
    if (errno)
        *errorString = errnoToQString(errno);
    return success;
}
/*
 * \internal
 * Returns a filename for a temporary file based on \a templateName
 */
static QString backupFileName(const QString &templateName)
{
    QTemporaryFile file(templateName);
    file.open();
    const QString name = file.fileName();
    file.close();
    file.remove();
    return name;
}

////////////////////////////////////////////////////////////////////////////
// KDUpdater::CopyOperation
////////////////////////////////////////////////////////////////////////////

CopyOperation::CopyOperation()
{
    setName(QLatin1String("Copy"));
}

CopyOperation::~CopyOperation()
{
    deleteFileNowOrLater(value(QLatin1String("backupOfExistingDestination")).toString());
}

void CopyOperation::backup()
{
    const QString dest = arguments().last();
    if (!QFile::exists(dest)) {
        clearValue(QLatin1String("backupOfExistingDestination"));
        return;
    }

    setValue(QLatin1String("backupOfExistingDestination"), backupFileName(dest));

    // race condition: The backup file could get created 
    // by another process right now. But this is the same 
    // in QFile::copy...
    const bool success = QFile::rename(dest, value(QLatin1String("backupOfExistingDestination")).toString());
    if (!success)
        setError(UserDefinedError, tr("Could not backup file %1.").arg(dest));
}

bool CopyOperation::performOperation()
{
    // We need two args to complete the copy operation.
    // First arg provides the complete file name of source
    // Second arg provides the complete file name of dest
    QStringList args = this->arguments();
    if (args.count() != 2) {
        setError(InvalidArguments);
        setErrorString(tr("Invalid arguments: %1 arguments given, 2 expected.").arg(args.count()));
        return false;
    }
    QString source = args.first();
    QString dest = args.last();

    // If destination file exists, then we cannot use QFile::copy()
    // because it does not overwrite an existing file. So we remove
    // the destination file.
    if (QFile::exists(dest)) {
        QFile file(dest);
        if (!file.remove()) {
            setError(UserDefinedError);
            setErrorString(tr("Could not remove destination file %1: %2").arg(dest, file.errorString()));
            return false;
        }
    }

    QFile file(source);
    const bool copied = file.copy(dest);
    if (!copied) {
        setError(UserDefinedError);
        setErrorString(tr("Could not copy %1 to %2: %3").arg(source, dest, file.errorString()));
    }
    return copied;
}

bool CopyOperation::undoOperation()
{
   const QString dest = arguments().last();

    QFile destF(dest);
    // first remove the dest
    if (!destF.remove()) {
        setError(UserDefinedError, tr("Could not delete file %1: %2").arg(dest, destF.errorString()));
        return false;
    }

    // no backup was done:
    // the copy destination file wasn't existing yet - that's no error
    if (!hasValue(QLatin1String("backupOfExistingDestination")))
        return true;

    QFile backupF(value(QLatin1String("backupOfExistingDestination")).toString());
    // otherwise we have to copy the backup back:
    const bool success = backupF.rename(dest);
    if (!success)
        setError(UserDefinedError, tr("Could not restore backup file into %1: %2").arg(dest, backupF.errorString()));
    return success;
}

/*!
 \reimp
 */
QDomDocument CopyOperation::toXml() const
{
    // we don't want to save the backupOfExistingDestination
    if (!hasValue(QLatin1String("backupOfExistingDestination")))
        return UpdateOperation::toXml();
    
    CopyOperation *const me = const_cast<CopyOperation *>(this);
    
    const QVariant v = value(QLatin1String("backupOfExistingDestination"));
    me->clearValue(QLatin1String("backupOfExistingDestination"));
    const QDomDocument xml = UpdateOperation::toXml();
    me->setValue(QLatin1String("backupOfExistingDestination"), v);
    return xml;
}

bool CopyOperation::testOperation()
{
    // TODO
    return true;
}

CopyOperation *CopyOperation::clone() const
{
    return new CopyOperation();
}


////////////////////////////////////////////////////////////////////////////
// KDUpdater::MoveOperation
////////////////////////////////////////////////////////////////////////////

MoveOperation::MoveOperation()
{
    setName(QLatin1String("Move"));
}

MoveOperation::~MoveOperation()
{
    deleteFileNowOrLater(value(QLatin1String("backupOfExistingDestination")).toString());
}

void MoveOperation::backup()
{
    const QString dest = arguments().last();
    if (!QFile::exists(dest)) {
        clearValue(QLatin1String("backupOfExistingDestination"));
        return;
    }

    setValue(QLatin1String("backupOfExistingDestination"), backupFileName(dest));

    // race condition: The backup file could get created 
    // by another process right now. But this is the same 
    // in QFile::copy...
    const bool success = QFile::rename(dest, value(QLatin1String("backupOfExistingDestination")).toString());
    if (!success)
        setError(UserDefinedError, tr("Could not backup file %1.").arg(dest));
}

bool MoveOperation::performOperation()
{
    // We need two args to complete the copy operation.
    // First arg provides the complete file name of source
    // Second arg provides the complete file name of dest
    QStringList args = this->arguments();
    if (args.count() != 2) {
        setError(InvalidArguments);
        setErrorString(tr("Invalid arguments: %1 arguments given, 2 expected.").arg(args.count()));
        return false;
    }

    QString source = args.first();
    QString dest = args.last();

    // If destination file exists, then we cannot use QFile::copy()
    // because it does not overwrite an existing file. So we remove
    // the destination file.
    if (QFile::exists(dest)) {
        QFile file(dest);
        if (!file.remove(dest)) {
            setError(UserDefinedError);
            setErrorString(tr("Could not remove destination file %1: %2").arg(dest, file.errorString()));
            return false;
        }
    }

    // Copy source to destination.
    QFile file(source);
    const bool copied = file.copy(source, dest);
    if (!copied) {
        setError(UserDefinedError);
        setErrorString(tr("Could not copy %1 to %2: %3").arg(source, dest, file.errorString()));
        return false;
    }

    return deleteFileNowOrLater(source);
}

bool MoveOperation::undoOperation()
{
    const QStringList args = arguments();
    const QString& source = args.first();
    const QString& dest = args.last();

    // first: copy back the destination to source
    QFile destF(dest);
    if (!destF.copy(source)) {
        setError(UserDefinedError, tr("Cannot copy %1 to %2: %3").arg(dest, source, destF.errorString()));
        return false;
    }

    // second: delete the move destination
    if (!deleteFileNowOrLater(dest)) {
        setError(UserDefinedError, tr("Cannot remove file %1."));
        return false;
    }

    // no backup was done:
    // the move destination file wasn't existing yet - that's no error
    if (!hasValue(QLatin1String("backupOfExistingDestination")))
        return true;

    // otherwise we have to copy the backup back:
    QFile backupF(value(QLatin1String("backupOfExistingDestination")).toString());
    const  bool success = backupF.rename(dest);
    if (!success)
        setError(UserDefinedError, tr("Cannot restore backup file for %1: %2").arg(dest, backupF.errorString()));

    return success;
}

bool MoveOperation::testOperation()
{
    // TODO
    return true;
}

MoveOperation *MoveOperation::clone() const
{
    return new MoveOperation;
}


////////////////////////////////////////////////////////////////////////////
// KDUpdater::DeleteOperation
////////////////////////////////////////////////////////////////////////////

DeleteOperation::DeleteOperation()
{
    setName(QLatin1String("Delete"));
}

DeleteOperation::~DeleteOperation()
{
    deleteFileNowOrLater(value(QLatin1String("backupOfExistingFile")).toString());
}

void DeleteOperation::backup()
{
    const QString fileName = arguments().first();
    setValue(QLatin1String("backupOfExistingFile"), backupFileName(fileName));
    QFile file(fileName);
    const bool success = file.copy(value(QLatin1String("backupOfExistingFile")).toString());
    if (!success)
        setError(UserDefinedError, tr("Cannot create backup of %1: %2").arg(fileName, file.errorString()));
}

bool DeleteOperation::performOperation()
{
    // Requires only one parameter. That is the name of
    // the file to remove.
    QStringList args = this->arguments();
    if (args.count() != 1) {
        setError(InvalidArguments);
        setErrorString(tr("Invalid arguments: %1 arguments given, 1 expected.").arg(args.count()));
        return false;
    }

    const QString fName = args.first();
    return deleteFileNowOrLater(fName);
}

bool DeleteOperation::undoOperation()
{
    if (!hasValue(QLatin1String("backupOfExistingFile")))
        return true;

    const QString fileName = arguments().first();
    QFile backupF(value(QLatin1String("backupOfExistingFile")).toString());
    const bool success = backupF.copy(fileName) && deleteFileNowOrLater(backupF.fileName());
    if (!success)
        setError(UserDefinedError, tr("Cannot restore backup file for %1: %2").arg(fileName, backupF.errorString()));

    return success;
}

bool DeleteOperation::testOperation()
{
    // TODO
    return true;
}

DeleteOperation *DeleteOperation::clone() const
{
    return new DeleteOperation;
}

/*!
 \reimp
 */
QDomDocument DeleteOperation::toXml() const
{
    // we don't want to save the backupOfExistingFile
    if (!hasValue(QLatin1String("backupOfExistingFile")))
        return UpdateOperation::toXml();
    
    DeleteOperation *const me = const_cast<DeleteOperation *>(this);
    
    const QVariant v = value(QLatin1String("backupOfExistingFile"));
    me->clearValue(QLatin1String("backupOfExistingFile"));
    const QDomDocument xml = UpdateOperation::toXml();
    me->setValue(QLatin1String("backupOfExistingFile"), v);
    return xml;
}

////////////////////////////////////////////////////////////////////////////
// KDUpdater::MkdirOperation
////////////////////////////////////////////////////////////////////////////

MkdirOperation::MkdirOperation()
{
    setName(QLatin1String("Mkdir"));
}

void MkdirOperation::backup()
{
    QString path = arguments().first();
    path.replace(QLatin1Char('\\'), QLatin1Char('/'));

    QDir createdDir = QDir::root();

    // find out, which part of the path is the first one we actually need to create
    int end = 0;
    while (true) {
        QString p = path.section(QLatin1Char('/'), 0, ++end);
        createdDir = QDir(p);
        if (!createdDir.exists())
            break;
        if (p == path) {
            // everything did already exist -> nothing to do for us (nothing to revert then, either)
            createdDir = QDir::root();
            break;
        }
    }

    setValue(QLatin1String("createddir"), createdDir.absolutePath());
}

bool MkdirOperation::performOperation()
{
    // Requires only one parameter. That is the name of
    // the file to remove.
    QStringList args = this->arguments();
    if (args.count() != 1) {
        setError(InvalidArguments);
        setErrorString(tr("Invalid arguments: %1 arguments given, 1 expected.").arg(args.count()));
        return false;
    }
    QString dirName = args.first();
    const bool created = QDir::root().mkpath(dirName);
    if (!created) {
        setError(UserDefinedError);
        setErrorString(tr("Could not create folder %1: Unknown error.").arg(dirName));
    }
    return created;
}

bool MkdirOperation::undoOperation()
{
    Q_ASSERT(arguments().count() == 1);

    QDir createdDir = QDir(value(QLatin1String("createddir")).toString());
    const bool forceremoval = QVariant(value(QLatin1String("forceremoval"))).toBool();

    // Since refactoring we know the mkdir operation which is creating the target path. If we do a full
    // uninstall prevent removing the full path including target, instead remove the target only. (QTIFW-46)
    if (hasValue(QLatin1String("uninstall-only")) && value(QLatin1String("uninstall-only")).toBool())
        createdDir = QDir(arguments().first());

    if (createdDir == QDir::root())
        return true;

    if (!createdDir.exists())
        return true;

    QString errorString;
    if (forceremoval)
        return removeDirectory(createdDir.path(), &errorString);

    // even remove some hidden, OS-created files in there
#if defined Q_OS_MAC
    QFile::remove(createdDir.path() + QLatin1String("/.DS_Store"));
#elif defined Q_OS_WIN
    QFile::remove(createdDir.path() + QLatin1String("/Thumbs.db"));
#endif

    errno = 0;
    const bool result = QDir::root().rmdir(createdDir.path());
    if (!result) {
        if (errorString.isEmpty())
            setError(UserDefinedError, tr("Cannot remove directory %1: %2").arg(createdDir.path(), errorString));
        else
            setError(UserDefinedError, tr("Cannot remove directory %1: %2").arg(createdDir.path(), errnoToQString(errno)));
    }
    return result;
}

bool KDUpdater::MkdirOperation::testOperation()
{
    // TODO
    return true;
}

MkdirOperation *MkdirOperation::clone() const
{
    return new MkdirOperation;
}

////////////////////////////////////////////////////////////////////////////
// KDUpdater::RmdirOperation
////////////////////////////////////////////////////////////////////////////

RmdirOperation::RmdirOperation()
{
    setValue(QLatin1String("removed"), false);
    setName(QLatin1String("Rmdir"));
}

void RmdirOperation::backup()
{
    // nothing to backup - rollback will just create the directory
}

bool RmdirOperation::performOperation()
{
    // Requires only one parameter. That is the name of
    // the file to remove.
    QStringList args = this->arguments();
    if (args.count() != 1) {
        setError(InvalidArguments);
        setErrorString(tr("Invalid arguments: %1 arguments given, 1 expected.").arg(args.count()));
        return false;
    }

    QString dirName = args.first();
    QDir dir(dirName);
    if (!dir.exists()) {
        setError(UserDefinedError);
        setErrorString(tr("Could not remove folder %1: The folder does not exist.").arg(dirName));
        return false;
    }

    errno = 0;
    const bool removed = dir.rmdir(dirName);
    setValue(QLatin1String("removed"), removed);
    if (!removed) {
        setError(UserDefinedError);
        setErrorString(tr("Could not remove folder %1: %2").arg(dirName, errnoToQString(errno)));
    }
    return removed;
}

bool RmdirOperation::undoOperation()
{
   if (!value(QLatin1String("removed")).toBool())
        return true;

    const QFileInfo fi(arguments().first());
    errno = 0;
    const bool success = fi.dir().mkdir(fi.fileName());
    if( !success)
        setError(UserDefinedError, tr("Cannot recreate directory %1: %2").arg(fi.fileName(), errnoToQString(errno)));

    return success;
}

bool RmdirOperation::testOperation()
{
    // TODO
    return true;
}

RmdirOperation *RmdirOperation::clone() const
{
    return new RmdirOperation;
}


////////////////////////////////////////////////////////////////////////////
// KDUpdater::AppendFileOperation
////////////////////////////////////////////////////////////////////////////

AppendFileOperation::AppendFileOperation()
{
    setName(QLatin1String("AppendFile"));
}

void AppendFileOperation::backup()
{
    const QString filename = arguments().first();

    QFile file(filename);
    if (!file.exists())
        return; // nothing to backup

    setValue(QLatin1String("backupOfFile"), backupFileName(filename));
    if (!file.copy(value(QLatin1String("backupOfFile")).toString())) {
        setError(UserDefinedError, tr("Cannot backup file %1: %2").arg(filename, file.errorString()));
        clearValue(QLatin1String("backupOfFile"));
    }
}

bool AppendFileOperation::performOperation()
{
    // This operation takes two arguments. First argument is the name
    // of the file into which a text has to be appended. Second argument
    // is the text to append.
    QStringList args = this->arguments();
    if (args.count() != 2) {
        setError(InvalidArguments);
        setErrorString(tr("Invalid arguments: %1 arguments given, 2 expected.").arg(args.count()));
        return false;
    }

    QString fName = args.first();
    QString text = args.last();

    QFile file(fName);
    if (!file.open(QFile::Append)) {
        // first we rename the file, then we copy it to the real target and open the copy - the renamed original is then marked for deletion
        const QString newName = backupFileName(fName);
        if (!QFile::rename(fName, newName) && QFile::copy(newName, fName) && file.open(QFile::Append)) {
            QFile::rename(newName, fName);
            setError(UserDefinedError);
            setErrorString(tr("Could not open file %1 for writing: %2").arg(file.fileName(), file.errorString()));
            return false;
        }
        deleteFileNowOrLater(newName);
    }

    QTextStream ts(&file);
    ts << text;
    file.close();

    return true;
}

bool AppendFileOperation::undoOperation()
{
   // backupOfFile being empty -> file didn't exist before -> no error
    const QString filename = arguments().first();
    const QString backupOfFile = value(QLatin1String("backupOfFile")).toString();
    if (!backupOfFile.isEmpty() && !QFile::exists(backupOfFile)) {
        setError(UserDefinedError, tr("Cannot find backup file for %1.").arg(filename));
        return false;
    }

    const bool removed = deleteFileNowOrLater(filename);
    if (!removed) {
        setError(UserDefinedError, tr("Could not restore backup file for %1.").arg(filename));
        return false;
    }

    // got deleted? We might be done, if it didn't exist before
    if (backupOfFile.isEmpty())
        return true;

    QFile backupFile(backupOfFile);
    const bool success = backupFile.rename(filename);
    if (!success)
        setError(UserDefinedError, tr("Could not restore backup file for %1: %2").arg(filename, backupFile.errorString()));
    return success;
}

bool AppendFileOperation::testOperation()
{
    // TODO
    return true;
}

AppendFileOperation *AppendFileOperation::clone() const
{
    return new AppendFileOperation;
}


////////////////////////////////////////////////////////////////////////////
// KDUpdater::PrependFileOperation
////////////////////////////////////////////////////////////////////////////

PrependFileOperation::PrependFileOperation()
{
    setName(QLatin1String("PrependFile"));
}

void PrependFileOperation::backup()
{
    const QString filename = arguments().first();

    QFile file(filename);
    if (!file.exists())
        return; // nothing to backup

    setValue(QLatin1String("backupOfFile"), backupFileName(filename));
    if (!file.copy(value(QLatin1String("backupOfFile")).toString())) {
        setError(UserDefinedError, tr("Cannot backup file %1: %2").arg(filename, file.errorString()));
        clearValue(QLatin1String("backupOfFile"));
    }
}

bool PrependFileOperation::performOperation()
{
    // This operation takes two arguments. First argument is the name
    // of the file into which a text has to be appended. Second argument
    // is the text to append.
    QStringList args = this->arguments();
    if (args.count() != 2) {
        setError(InvalidArguments);
        setErrorString(tr("Invalid arguments: %1 arguments given, 2 expected.").arg(args.count()));
        return false;
    }

    QString fName = args.first();
    QString text = args.last();

    // Load the file first.
    QFile file(fName);
    if (!file.open(QFile::ReadOnly)) {
        setError(UserDefinedError);
        setErrorString(tr("Could not open file %1 for reading: %2").arg(file.fileName(), file.errorString()));
        return false;
    }
    QString fContents(QLatin1String(file.readAll()));
    file.close();

    // Prepend text to the file text
    fContents = text + fContents;

    // Now re-open the file in write only mode.
    if (!file.open(QFile::WriteOnly)) {
        // first we rename the file, then we copy it to the real target and open the copy - the renamed original is then marked for deletion
        const QString newName = backupFileName(fName);
        if (!QFile::rename(fName, newName) && QFile::copy(newName, fName) && file.open(QFile::WriteOnly)) {
            QFile::rename(newName, fName);
            setError(UserDefinedError);
            setErrorString(tr("Could not open file %1 for writing: %2").arg(file.fileName(), file.errorString()));
            return false;
        }
        deleteFileNowOrLater(newName);
    }
    QTextStream ts(&file);
    ts << fContents;
    file.close();

    return true;
}

bool PrependFileOperation::undoOperation()
{
    // bockupOfFile being empty -> file didn't exist before -> no error
    const QString filename = arguments().first();
    const QString backupOfFile = value(QLatin1String("backupOfFile")).toString();
    if (!backupOfFile.isEmpty() && !QFile::exists(backupOfFile)) {
        setError(UserDefinedError, tr("Cannot find backup file for %1.").arg(filename));
        return false;
    }

    if (!deleteFileNowOrLater(filename)) {
        setError(UserDefinedError, tr("Cannot restore backup file for %1.").arg(filename));
        return false;
    }

    // got deleted? We might be done, if it didn't exist before
    if (backupOfFile.isEmpty())
        return true;

    QFile backupF(backupOfFile);
    const bool success = backupF.rename(filename);
    if (!success)
        setError(UserDefinedError, tr("Cannot restore backup file for %1: %2").arg(filename, backupF.errorString()));

    return success;
}

bool PrependFileOperation::testOperation()
{
    // TODO
    return true;
}

PrependFileOperation *PrependFileOperation::clone() const
{
    return new PrependFileOperation;
}


////////////////////////////////////////////////////////////////////////////
// KDUpdater::ExecuteOperation
////////////////////////////////////////////////////////////////////////////

ExecuteOperation::ExecuteOperation()
    : QObject()
{
    setName(QLatin1String("Execute"));
}

void ExecuteOperation::backup()
{
    // this is not possible, since the process can do whatever...
}

#if defined(SUPPORT_DETACHED_PROCESS_EXECUTION) && defined(Q_OS_WIN)
// stolen from qprocess_win.cpp
static QString qt_create_commandline(const QString &program, const QStringList &arguments)
{
    QString args;
    if (!program.isEmpty()) {
        QString programName = program;
        if (!programName.startsWith(QLatin1Char('\"')) && !programName.endsWith(QLatin1Char('\"')) && programName.contains(QLatin1Char(' ')))
            programName = QLatin1Char('\"') + programName + QLatin1Char('\"');
        programName.replace(QLatin1Char('/'), QLatin1Char('\\'));

        // add the prgram as the first arg ... it works better
        args = programName + QLatin1Char(' ');
    }

    for (int i = 0; i < arguments.size(); ++i) {
        QString tmp = arguments.at(i);
        // in the case of \" already being in the string the \ must also be escaped
        tmp.replace(QLatin1String("\\\""), QLatin1String("\\\\\""));
        // escape a single " because the arguments will be parsed
        tmp.replace(QLatin1Char('\"'), QLatin1String("\\\""));
        if (tmp.isEmpty() || tmp.contains(QLatin1Char(' ')) || tmp.contains(QLatin1Char('\t'))) {
            // The argument must not end with a \ since this would be interpreted
            // as escaping the quote -- rather put the \ behind the quote: e.g.
            // rather use "foo"\ than "foo\"
            QString endQuote(QLatin1Char('\"'));
            int i = tmp.length();
            while (i > 0 && tmp.at(i - 1) == QLatin1Char('\\')) {
                --i;
                endQuote += QLatin1Char('\\');
            }
            args += QLatin1String(" \"") + tmp.left(i) + endQuote;
        } else {
            args += QLatin1Char(' ') + tmp;
        }
    }
    return args;
}
#endif

bool ExecuteOperation::performOperation()
{
    // This operation receives only one argument. It is the complete
    // command line of the external program to execute.
    QStringList args = this->arguments();
    if (args.isEmpty()) {
        setError(InvalidArguments);
        setErrorString(tr("Invalid arguments: %1 arguments given, 2 expected.").arg(args.count()));
        return false;
    }

    QList<int> allowedExitCodes;
    
    QRegExp re(QLatin1String("^\\{((-?\\d+,)*-?\\d+)\\}$"));
    if (re.exactMatch(args.first())) {
        const QStringList numbers = re.cap(1).split(QLatin1Char(','));
        for (QStringList::const_iterator it = numbers.begin(); it != numbers.end(); ++it)
            allowedExitCodes.push_back(it->toInt());
        args.pop_front();
    } else {
        allowedExitCodes.push_back(0);
    }

    bool success = false;
#ifdef SUPPORT_DETACHED_PROCESS_EXECUTION
    // unix style: when there's an ampersand after the command, it's started detached
    if (args.count() >= 2 && args.last() == QLatin1String("&")) {
        args.pop_back();
#ifdef Q_OS_WIN
        QString arguments = qt_create_commandline(args.front(), args.mid(1));
        
        PROCESS_INFORMATION pinfo;

        STARTUPINFOW startupInfo = { sizeof(STARTUPINFO), 0, 0, 0,
                                     static_cast< ulong >(CW_USEDEFAULT), static_cast< ulong >(CW_USEDEFAULT),
                                     static_cast< ulong >(CW_USEDEFAULT), static_cast< ulong >(CW_USEDEFAULT),
                                     0, 0, 0, STARTF_USESHOWWINDOW, SW_HIDE, 0, 0, 0, 0, 0
                                   };
        success = CreateProcess(0, (wchar_t*)arguments.utf16(),
                                0, 0, false, CREATE_UNICODE_ENVIRONMENT | CREATE_NEW_CONSOLE, 0,
                                0,
                                &startupInfo, &pinfo);

#else
        success = QProcess::startDetached(args.front(), args.mid(1));
#endif
    }
    else
#endif
    {
        Environment::instance().applyTo(&process); //apply non-persistent variables
        process.start(args.front(), args.mid(1));
        
        QEventLoop loop;
        QObject::connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit()));
        QObject::connect(&process, SIGNAL(readyRead()), this, SLOT(readProcessOutput()));
        success = process.waitForStarted(-1);
        if (success) {
            loop.exec();
            setValue(QLatin1String("ExitCode"), process.exitCode());
            success = allowedExitCodes.contains(process.exitCode());
        }
    }
    if (!success) {
        setError(UserDefinedError);
        setErrorString(tr("Execution failed: %1").arg(args.join(QLatin1String(" "))));
    }

    return success;
}

/*!
 Cancels the ExecuteOperation. This methods tries to terminate the process
 gracefully by calling QProcess::terminate. After 10 seconds, the process gets killed.
 */
void ExecuteOperation::cancelOperation()
{
    if (process.state() == QProcess::Running)
        process.terminate();
    if (!process.waitForFinished(10000))
        process.kill();
}

void ExecuteOperation::readProcessOutput()
{
    QByteArray output = process.readAll();
    if (!output.isEmpty())
        emit outputTextChanged(QString::fromLocal8Bit(output));
}

bool ExecuteOperation::undoOperation()
{
    // this is not possible, since the process can do whatever...
    return false;
}

bool ExecuteOperation::testOperation()
{
    // TODO
    return true;
}

ExecuteOperation *ExecuteOperation::clone() const
{
    return new ExecuteOperation;
}


////////////////////////////////////////////////////////////////////////////
// KDUpdater::UpdatePackageOperation
////////////////////////////////////////////////////////////////////////////

UpdatePackageOperation::UpdatePackageOperation()
{
    setName(QLatin1String("UpdatePackage"));
}

void UpdatePackageOperation::backup()
{
    const PackageInfo info = application()->packagesInfo()->packageInfo(application()->packagesInfo()->findPackageInfo(arguments().first()));
    setValue(QLatin1String("oldVersion"), info.version);
    setValue(QLatin1String("oldDate"), info.lastUpdateDate);
}

bool UpdatePackageOperation::performOperation()
{
    // This operation receives three arguments : the name of the package
    // the new version and the release date
    const QStringList args = this->arguments();
    if (args.count() != 3) {
        setError(InvalidArguments, tr("Invalid arguments: %1 arguments given, 3 expected.").arg(args.count()));
        return false;
    }

    const QString &packageName = args.at(0);
    const QString &version = args.at(1);
    const QDate date = QDate::fromString(args.at(2));
    const bool success = application()->packagesInfo()->updatePackage(packageName, version, date);
    if (!success)
        setError(UserDefinedError, tr("Cannot update %1-%2").arg(packageName, version));

    return success;
}

bool UpdatePackageOperation::undoOperation()
{
    const QString packageName = arguments().first();
    const QString version = arguments().at(1);
    const QString oldVersion = value(QLatin1String("oldVersion")).toString();
    const QDate oldDate = value(QLatin1String("oldDate")).toDate();
    const bool success = application()->packagesInfo()->updatePackage(packageName, oldVersion, oldDate);
    if (!success)
        setError(UserDefinedError, tr("Cannot restore %1-%2").arg(packageName, version));

    return success;
}

bool UpdatePackageOperation::testOperation()
{
    // TODO
    return true;
}

UpdatePackageOperation *UpdatePackageOperation::clone() const
{
    return new UpdatePackageOperation;
}


////////////////////////////////////////////////////////////////////////////
// KDUpdater::UpdateCompatOperation
////////////////////////////////////////////////////////////////////////////

UpdateCompatOperation::UpdateCompatOperation()
{
    setName(QLatin1String("UpdateCompatLevel"));
}

void UpdateCompatOperation::backup()
{
    setValue(QLatin1String("oldCompatLevel"), application()->packagesInfo()->compatLevel());
}

bool UpdateCompatOperation::performOperation()
{
    // This operation receives one argument : the new compat level
    const QStringList args = this->arguments();
    if (args.count() != 1) {
        setError(InvalidArguments, tr("Invalid arguments: %1 arguments given, 1 expected.").arg(args.count()));
        return false;
    }

    const int level = args.first().toInt();
    application()->packagesInfo()->setCompatLevel(level);
    return true;
}

bool UpdateCompatOperation::undoOperation()
{
    if (!hasValue(QLatin1String("oldCompatLevel"))) {
        setError(UserDefinedError, tr("Cannot restore previous compat-level"));
        return false;
    }

    application()->packagesInfo()->setCompatLevel(value(QLatin1String("oldCompatLevel")).toInt());
    return true;
}

bool UpdateCompatOperation::testOperation()
{
    // TODO
    return true;
}

UpdateCompatOperation *UpdateCompatOperation::clone() const
{
    return new UpdateCompatOperation;
}