main.cpp 8.97 KB
Newer Older
1
2
3
4
/*
 * ark -- archiver for the KDE project
 *
 * Copyright (C) 2007 Henrique Pinto <henrique.pinto@kdemail.net>
5
 * Copyright (C) 2008-2009 Harald Hvaal <haraldhv@stud.ntnu.no>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * 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; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
22

23
24
25
26
27
#include "mainwindow.h"
#include "kerfuffle/batchextract.h"
#include "kerfuffle/addtoarchive.h"

#include <KAboutData>
Henrique Pinto's avatar
Henrique Pinto committed
28
29
#include <KApplication>
#include <KCmdLineArgs>
30
#include <KDebug>
Henrique Pinto's avatar
Henrique Pinto committed
31
#include <KLocale>
32

Henrique Pinto's avatar
Henrique Pinto committed
33
#include <QByteArray>
34
#include <QFileInfo>
35
#include <QEventLoop>
Henrique Pinto's avatar
Henrique Pinto committed
36

37
using Kerfuffle::AddToArchive;
38
using Kerfuffle::BatchExtract;
Henrique Pinto's avatar
Henrique Pinto committed
39

40
int main(int argc, char **argv)
Henrique Pinto's avatar
Henrique Pinto committed
41
{
42
    KAboutData aboutData("ark", 0, ki18n("Ark"),
43
                         "2.14", ki18n("KDE Archiving tool"),
44
                         KAboutData::License_GPL,
45
                         ki18n("(c) 1997-2009, The Various Ark Developers"),
46
47
48
49
50
51
52
                         KLocalizedString(),
                         "http://utils.kde.org/projects/ark"
                        );

    aboutData.addAuthor(ki18n("Harald Hvaal"),
                        ki18n("Maintainer"),
                        "haraldhv@stud.ntnu.no");
53
54
55
    aboutData.addAuthor(ki18n("Raphael Kubo da Costa"),
                        ki18n("Maintainer"),
                        "kubito@gmail.com");
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
    aboutData.addAuthor(ki18n("Henrique Pinto"),
                        ki18n("Former Maintainer"),
                        "henrique.pinto@kdemail.net");
    aboutData.addAuthor(ki18n("Helio Chissini de Castro"),
                        ki18n("Former maintainer"),
                        "helio@kde.org");
    aboutData.addAuthor(ki18n("Georg Robbers"),
                        KLocalizedString(),
                        "Georg.Robbers@urz.uni-hd.de");
    aboutData.addAuthor(ki18n("Roberto Selbach Teixeira"),
                        KLocalizedString(),
                        "maragato@kde.org");
    aboutData.addAuthor(ki18n("Francois-Xavier Duranceau"),
                        KLocalizedString(),
                        "duranceau@kde.org");
    aboutData.addAuthor(ki18n("Emily Ezust (Corel Corporation)"),
                        KLocalizedString(),
                        "emilye@corel.com");
    aboutData.addAuthor(ki18n("Michael Jarrett (Corel Corporation)"),
                        KLocalizedString(),
                        "michaelj@corel.com");
    aboutData.addAuthor(ki18n("Robert Palmbos"),
                        KLocalizedString(),
                        "palm9744@kettering.edu");

    aboutData.addCredit(ki18n("Bryce Corkins"),
                        ki18n("Icons"),
                        "dbryce@attglobal.net");
    aboutData.addCredit(ki18n("Liam Smit"),
                        ki18n("Ideas, help with the icons"),
                        "smitty@absamail.co.za");
    aboutData.addCredit(ki18n("Andrew Smith"),
                        ki18n("bkisofs code"),
                        QByteArray(),
                        "http://littlesvr.ca/misc/contactandrew.php");
    aboutData.setProgramIconName("utilities-file-archiver");

    KCmdLineArgs::init(argc, argv, &aboutData);
    KCmdLineOptions option;
    option.add("+[url]", ki18n("URL of an archive to be opened"));
    option.add("d").add("dialog", ki18n("Show a dialog for specifying the options for the operation (extract/add)"));
    option.add("o").add("destination <directory>", ki18n("Destination folder to extract to. Defaults to current path if not specified."));
    option.add(":", ki18n("Options for adding files"));
    option.add("c").add("add", ki18n("Query the user for an archive filename and add specified files to it. Quit when finished."));
    option.add("t").add("add-to <filename>", ki18n("Add the specified files to 'filename'. Create archive if it does not exist. Quit when finished."));
    option.add("p").add("changetofirstpath", ki18n("Change the current dir to the first entry and add all other entries relative to this one."));
    option.add("f").add("autofilename <suffix>", ki18n("Automatically choose a filename, with the selected suffix (for example rar, tar.gz, zip or any other supported types)"));
    option.add(":", ki18n("Options for batch extraction:"));
    option.add("b").add("batch", ki18n("Use the batch interface instead of the usual dialog. This option is implied if more than one url is specified."));
    option.add("e").add("autodestination", ki18n("The destination argument will be set to the path of the first file supplied."));
    option.add("a").add("autosubfolder", ki18n("Archive contents will be read, and if detected to not be a single folder archive, a subfolder by the name of the archive will be created."));
    option.add("s").add("subfolder <directory>", ki18n("Create a subfolder under the destination directory and extract here."));
    KCmdLineArgs::addCmdLineOptions(option);
    KCmdLineArgs::addTempFileOption();

    KApplication application;
    application.setQuitOnLastWindowClosed(false);

    //session restoring
    if (application.isSessionRestored()) {
        MainWindow* window = NULL;

        if (KMainWindow::canBeRestored(1)) {
            window = new MainWindow;
            window->restore(1);
            if (!window->loadPart()) {
                delete window;
                window = NULL;
            }
        }

        if (window == NULL) {
            return -1;
        }
    } else { //new ark window (no restored session)
        // open any given URLs
        KCmdLineArgs *args = KCmdLineArgs::parsedArgs();

        if (args->isSet("add") || args->isSet("add-to")) {
135
136
            AddToArchive *addToArchiveJob = new AddToArchive;
            application.connect(addToArchiveJob, SIGNAL(result(KJob*)), SLOT(quit()));
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

            if (args->isSet("changetofirstpath")) {
                addToArchiveJob->setChangeToFirstPath(true);
            }

            if (args->isSet("add-to")) {
                addToArchiveJob->setFilename(args->getOption("add-to"));
            }

            if (args->isSet("autofilename")) {
                addToArchiveJob->setAutoFilenameSuffix(args->getOption("autofilename"));
            }

            for (int i = 0; i < args->count(); ++i) {
                //TODO: use the returned value here?
                addToArchiveJob->addInput(args->url(i));
            }

            if (args->isSet("dialog")) {
                if (!addToArchiveJob->showAddDialog()) {
                    return 0;
                }
            }

            addToArchiveJob->start();
        } else if (args->isSet("batch")) {
            //once the job has been started this interface can be safely
            //deleted
165
166
            BatchExtract *batchJob = new BatchExtract;
            application.connect(batchJob, SIGNAL(result(KJob*)), SLOT(quit()));
167
168
169
170
171
172

            for (int i = 0; i < args->count(); ++i) {
                batchJob->addInput(args->url(i));
            }

            if (args->isSet("autosubfolder")) {
173
                kDebug() << "Setting autosubfolder";
174
175
176
177
                batchJob->setAutoSubfolder(true);
            }

            if (args->isSet("subfolder")) {
178
                kDebug() << "Setting subfolder to " << args->getOption("subfolder");
179
180
181
182
183
                batchJob->setSubfolder(args->getOption("subfolder"));
            }

            if (args->isSet("autodestination")) {
                QString autopath = QFileInfo(args->url(0).path()).path();
184
                kDebug() << "By autodestination, setting path to " << autopath;
185
186
187
188
                batchJob->setDestinationFolder(autopath);
            }

            if (args->isSet("destination")) {
189
                kDebug() << "Setting destination to " << args->getOption("destination");
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
                batchJob->setDestinationFolder(args->getOption("destination"));
            }

            if (args->isSet("dialog")) {
                if (!batchJob->showExtractDialog()) {
                    return 0;
                }
            }

            batchJob->start();
        } else {
            MainWindow *window = new MainWindow;
            if (!window->loadPart()) { // if loading the part fails
                return -1;
            }

            if (args->count()) {
                kDebug() << "trying to open" << args->url(0);

                if (args->isSet("dialog")) {
                    window->setShowExtractDialog(true);
                }
                window->openUrl(args->url(0));
            }
            window->show();
        }
    }

218
    kDebug() << "Entering application loop";
219
    return application.exec();
Henrique Pinto's avatar
Henrique Pinto committed
220
}