Archiver.hxx 6.26 KB
Newer Older
1
//**************************************************************************
2
//   Copyright 2006 - 2017 Martin Koller, kollix@aon.at
3 4 5 6 7 8
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, version 2 of the License
//
//**************************************************************************
Martin Koller's avatar
Martin Koller committed
9

Martin Koller's avatar
Martin Koller committed
10 11
#ifndef _ARCHIVER_H_
#define _ARCHIVER_H_
Martin Koller's avatar
Martin Koller committed
12 13 14

// the class which does the archiving

Martin Koller's avatar
Martin Koller committed
15 16
#include <QObject>
#include <QPointer>
Martin Koller's avatar
Martin Koller committed
17
#include <QSet>
Martin Koller's avatar
Martin Koller committed
18
#include <QElapsedTimer>
Martin Koller's avatar
Martin Koller committed
19
#include <QTime>
Martin Koller's avatar
Martin Koller committed
20
#include <QDateTime>
Martin Koller's avatar
Martin Koller committed
21
#include <QStringList>
Martin Koller's avatar
Martin Koller committed
22 23 24
#include <QList>
#include <QRegExp>

Martin Koller's avatar
Martin Koller committed
25
#include <QUrl>
Martin Koller's avatar
Martin Koller committed
26
#include <kio/copyjob.h>
Martin Koller's avatar
Martin Koller committed
27
#include <kio/udsentry.h>
28
#include <KCompressionDevice>
Martin Koller's avatar
Martin Koller committed
29 30 31 32

class KTar;
class QDir;
class QFileInfo;
Martin Koller's avatar
Martin Koller committed
33
class QFile;
Martin Koller's avatar
Martin Koller committed
34 35 36 37 38 39 40


class Archiver : public QObject
{
  Q_OBJECT

  public:
Martin Koller's avatar
Martin Koller committed
41
    explicit Archiver(QWidget *parent);
Martin Koller's avatar
Martin Koller committed
42

Martin Koller's avatar
Martin Koller committed
43 44
    static Archiver *instance;

45
    // always call after you have already set maxSliceMBs, as the sliceCapacity
46
    // might be limited with it
Martin Koller's avatar
Martin Koller committed
47 48
    void setTarget(const QUrl &target);
    const QUrl &getTarget() const { return targetURL; }
49 50 51 52 53 54 55 56

    enum { UNLIMITED = 0 };
    void setMaxSliceMBs(int mbs);
    int getMaxSliceMBs() const { return maxSliceMBs; }

    void setFilePrefix(const QString &prefix);
    const QString &getFilePrefix() const { return filePrefix; }

57 58 59 60
    void setMediaNeedsChange(bool b) { mediaNeedsChange = b; }
    bool getMediaNeedsChange() const { return mediaNeedsChange; }

    void setCompressFiles(bool b);
Martin Koller's avatar
Martin Koller committed
61 62 63 64 65 66
    bool getCompressFiles() const { return !ext.isEmpty(); }

    // number of backups to keep before older ones will be deleted (UNLIMITED or 1..n)
    void setKeptBackups(int num);
    int getKeptBackups() const { return numKeptBackups; }

Martin Koller's avatar
Martin Koller committed
67 68 69 70 71
    // define a filename filter in wildcard format each separated with a space
    // e.g. "*.png *.ogg"
    void setFilter(const QString &filter);
    QString getFilter() const;

Martin Koller's avatar
Martin Koller committed
72 73 74 75
    // define a list of path wildcards to filter complete dirs, separated by newline
    void setDirFilter(const QString &filter);
    QString getDirFilter() const;

Martin Koller's avatar
Martin Koller committed
76 77 78 79 80 81 82
    // interval for a full backup instead differential backup; when 1 given == full backup
    void setFullBackupInterval(int days);
    int getFullBackupInterval() const { return fullBackupInterval; }

    const QDateTime &getLastFullBackup() const { return lastFullBackup; }
    const QDateTime &getLastBackup() const { return lastBackup; }

Martin Koller's avatar
Martin Koller committed
83 84
    // print every single file/dir in non-interactive mode
    void setVerbose(bool b) { verbose = b; }
85 86 87 88

    // loads the profile into the Archiver and returns includes/excludes lists
    // return true if loaded, false on file open error
    bool loadProfile(const QString &fileName, QStringList &includes, QStringList &excludes, QString &error);
Martin Koller's avatar
Martin Koller committed
89 90 91
    void setLoadedProfile(const QString &fileName) { loadedProfile = fileName; }

    bool saveProfile(const QString &fileName, const QStringList &includes, const QStringList &excludes, QString &error);
92 93 94

    // return true if the backup completed successfully, else false
    bool createArchive(const QStringList &includes, const QStringList &excludes);
Martin Koller's avatar
Martin Koller committed
95 96

    KIO::filesize_t getTotalBytes() const { return totalBytes; }
Martin Koller's avatar
Martin Koller committed
97 98 99
    int getTotalFiles() const { return totalFiles; }

    bool isInProgress() const { return runs; }
Martin Koller's avatar
Martin Koller committed
100 101 102 103 104 105

    static bool getDiskFree(const QString &path, KIO::filesize_t &capacityBytes, KIO::filesize_t &freeBytes);

    // TODO: put probably in some global settings object
    static QString sliceScript;

Laurent Montel's avatar
Laurent Montel committed
106
  public Q_SLOTS:
Martin Koller's avatar
Martin Koller committed
107
    void cancel();  // cancel a running creation
Martin Koller's avatar
Martin Koller committed
108
    void setForceFullBackup(bool force = true);
Martin Koller's avatar
Martin Koller committed
109

Laurent Montel's avatar
Laurent Montel committed
110
  Q_SIGNALS:
Laurent Montel's avatar
Laurent Montel committed
111 112 113 114 115 116 117 118 119 120 121
    void inProgress(bool runs);
    void logging(const QString &);
    void warning(const QString &);
    void targetCapacity(KIO::filesize_t bytes);
    void sliceProgress(int percent);
    void fileProgress(int percent);
    void newSlice(int);
    void totalFilesChanged(int);
    void totalBytesChanged(KIO::filesize_t);
    void elapsedChanged(const QTime &);
    void backupTypeChanged(bool incremental);
Martin Koller's avatar
Martin Koller committed
122

Laurent Montel's avatar
Laurent Montel committed
123
  private Q_SLOTS:
Martin Koller's avatar
Martin Koller committed
124 125
    void slotResult(KJob *);
    void slotListResult(KIO::Job *, const KIO::UDSEntryList &);
126
    void receivedOutput();
127 128
    void loggingSlot(const QString &message); // for non-interactive output
    void warningSlot(const QString &message); // for non-interactive output
Martin Koller's avatar
Martin Koller committed
129
    void updateElapsed();
Martin Koller's avatar
Martin Koller committed
130 131

  private:
132
    void calculateCapacity();  // also emits signals
Martin Koller's avatar
Martin Koller committed
133 134
    void addDirFiles(QDir &dir);
    void addFile(const QFileInfo &info);
Martin Koller's avatar
Martin Koller committed
135 136 137 138

    enum AddFileStatus { Error, Added, Skipped };
    AddFileStatus addLocalFile(const QFileInfo &info);

Martin Koller's avatar
Martin Koller committed
139
    bool compressFile(const QString &origName, QFile &comprFile);
Martin Koller's avatar
Martin Koller committed
140 141 142 143 144

    void finishSlice();
    bool getNextSlice();

    void runScript(const QString &mode);
Martin Koller's avatar
Martin Koller committed
145 146 147 148
    void setIncrementalBackup(bool inc);

    // returns true if the next backup will be an incremental one, false for a full backup
    bool isIncrementalBackup() const { return !forceFullBackup && incrementalBackup; }
Martin Koller's avatar
Martin Koller committed
149

Martin Koller's avatar
Martin Koller committed
150 151 152
    // return true if given fileName matches any of the defined filters
    bool fileIsFiltered(const QString &fileName) const;

Laurent Montel's avatar
Laurent Montel committed
153
    void emitArchiveError();
Martin Koller's avatar
Martin Koller committed
154

155
    static bool UDSlessThan(const KIO::UDSEntry &left, const KIO::UDSEntry &right);
Martin Koller's avatar
Martin Koller committed
156

Martin Koller's avatar
Martin Koller committed
157
  private:
Martin Koller's avatar
Martin Koller committed
158 159
    QSet<QString> excludeFiles;
    QSet<QString> excludeDirs;
Martin Koller's avatar
Martin Koller committed
160 161

    QString archiveName;
162
    QString filePrefix;  // default = "backup"
Martin Koller's avatar
Martin Koller committed
163
    QStringList sliceList;
Martin Koller's avatar
Martin Koller committed
164
    QString loadedProfile;
Martin Koller's avatar
Martin Koller committed
165 166 167

    KTar *archive;
    KIO::filesize_t totalBytes;
Martin Koller's avatar
Martin Koller committed
168
    int totalFiles;
Martin Koller's avatar
Martin Koller committed
169
    int filteredFiles;  // filter or time filter (incremental backup)
Martin Koller's avatar
Martin Koller committed
170
    QElapsedTimer elapsed;
Martin Koller's avatar
Martin Koller committed
171

Martin Koller's avatar
Martin Koller committed
172
    QList<QRegExp> filters;
Martin Koller's avatar
Martin Koller committed
173
    QList<QRegExp> dirFilters;
Martin Koller's avatar
Martin Koller committed
174

Martin Koller's avatar
Martin Koller committed
175
    QUrl targetURL;
Martin Koller's avatar
Martin Koller committed
176 177
    QString baseName;
    int sliceNum;
178
    int maxSliceMBs;
179 180
    bool mediaNeedsChange;
    bool compressFiles;
181

Martin Koller's avatar
Martin Koller committed
182 183 184
    int numKeptBackups;
    KIO::UDSEntryList targetDirList;

Martin Koller's avatar
Martin Koller committed
185 186 187 188 189 190
    QDateTime lastFullBackup;
    QDateTime lastBackup;
    int fullBackupInterval;
    bool incrementalBackup;
    bool forceFullBackup;

Martin Koller's avatar
Martin Koller committed
191 192 193 194
    KIO::filesize_t sliceBytes;
    KIO::filesize_t sliceCapacity;

    QString ext;
195
    KCompressionDevice::CompressionType compressionType;
Martin Koller's avatar
Martin Koller committed
196

197
    bool interactive;
Martin Koller's avatar
Martin Koller committed
198
    bool cancelled;
Martin Koller's avatar
Martin Koller committed
199
    bool runs;
200
    bool skippedFiles;  // did we skip files during backup ?
Martin Koller's avatar
Martin Koller committed
201
    bool verbose;
Martin Koller's avatar
Martin Koller committed
202

Martin Koller's avatar
Martin Koller committed
203
    QPointer<KIO::CopyJob> job;
Martin Koller's avatar
Martin Koller committed
204 205 206 207
    int jobResult;
};

#endif