]> git.sesse.net Git - kdenlive/blob - src/archivewidget.cpp
New feature: Archive project (in progress)
[kdenlive] / src / archivewidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "archivewidget.h"
22 #include "titlewidget.h"
23
24 #include <KLocale>
25 #include <KDiskFreeSpaceInfo>
26 #include <KUrlRequester>
27 #include <KFileDialog>
28
29 #include <KDebug>
30 #include <QListWidget>
31 #include "projectsettings.h"
32
33
34 ArchiveWidget::ArchiveWidget(QList <DocClipBase*> list, QStringList lumas, QWidget * parent) :
35         QDialog(parent),
36         m_requestedSize(0),
37         m_copyJob(NULL)
38 {
39     setupUi(this);
40     setWindowTitle(i18n("Archive Project"));
41     archive_url->setUrl(KUrl(QDir::homePath()));
42     connect(archive_url, SIGNAL(textChanged (const QString &)), this, SLOT(slotCheckSpace()));
43
44     // process all files
45     QStringList allFonts;
46     foreach(const QString & file, lumas) {
47         kDebug()<<"LUMA: "<<file;
48         files_list->addItem(file);
49         m_requestedSize += QFileInfo(file).size();
50     }
51
52     for (int i = 0; i < list.count(); i++) {
53         DocClipBase *clip = list.at(i);
54         if (clip->clipType() == SLIDESHOW) {    
55             QStringList subfiles = ProjectSettings::extractSlideshowUrls(clip->fileURL());
56             foreach(const QString & file, subfiles) {
57                 kDebug()<<"SLIDE: "<<file;
58                 files_list->addItem(file);
59                 m_requestedSize += QFileInfo(file).size();
60             }
61         } else if (!clip->fileURL().isEmpty()) {
62             files_list->addItem(clip->fileURL().path());
63             m_requestedSize += QFileInfo(clip->fileURL().path()).size();
64         }
65         if (clip->clipType() == TEXT) {
66             QStringList imagefiles = TitleWidget::extractImageList(clip->getProperty("xmldata"));
67             QStringList fonts = TitleWidget::extractFontList(clip->getProperty("xmldata"));
68             foreach(const QString & file, imagefiles) {
69                 kDebug()<<"TXT IMAGE: "<<file;
70                 files_list->addItem(file);
71                 m_requestedSize += QFileInfo(file).size();
72             }
73             allFonts << fonts;
74         } else if (clip->clipType() == PLAYLIST) {
75             QStringList files = ProjectSettings::extractPlaylistUrls(clip->fileURL().path());
76             foreach(const QString & file, files) {
77                 kDebug()<<"PLAYLIST: "<<file;
78                 files_list->addItem(file);
79                 m_requestedSize += QFileInfo(file).size();
80             }
81         }
82     }
83 #if QT_VERSION >= 0x040500
84     allFonts.removeDuplicates();
85 #endif
86     project_files->setText(i18n("%1 files to archive, requires %2", files_list->count(), KIO::convertSize(m_requestedSize)));
87     buttonBox->button(QDialogButtonBox::Apply)->setText(i18n("Archive"));
88     connect(buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(slotStartArchiving()));
89     buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
90     
91     slotCheckSpace();
92 }
93
94 ArchiveWidget::~ArchiveWidget()
95 {
96 }
97
98 void ArchiveWidget::slotCheckSpace()
99 {
100     KDiskFreeSpaceInfo inf = KDiskFreeSpaceInfo::freeSpaceInfo( archive_url->url().path());
101     KIO::filesize_t freeSize = inf.available();;
102     if (freeSize > m_requestedSize) {
103         // everything is ok
104         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true);
105         icon_info->setPixmap(KIcon("dialog-ok").pixmap(16, 16));
106         text_info->setText(i18n("Available space on drive: %1", KIO::convertSize(freeSize)));
107     }
108     else {
109         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
110         icon_info->setPixmap(KIcon("dialog-close").pixmap(16, 16));
111         text_info->setText(i18n("Not enough space on drive, free space: %1", KIO::convertSize(freeSize)));
112     }
113 }
114
115 void ArchiveWidget::slotStartArchiving()
116 {
117     if (m_copyJob) {
118         // archiving in progress, abort
119         m_copyJob->kill(KJob::EmitResult);
120         return;
121     }
122     KUrl::List files;
123     for (int i = 0; i < files_list->count(); i++) {
124         if (files_list->item(i))
125             files << KUrl(files_list->item(i)->text());
126     }
127     
128     progressBar->setValue(0);
129     buttonBox->button(QDialogButtonBox::Apply)->setText(i18n("Abort"));
130     m_copyJob = KIO::copy (files, archive_url->url(), KIO::HideProgressInfo);
131     connect(m_copyJob, SIGNAL(result(KJob *)), this, SLOT(slotArchivingFinished(KJob *)));
132     connect(m_copyJob, SIGNAL(processedSize(KJob *, qulonglong)), this, SLOT(slotArchivingProgress(KJob *, qulonglong)));
133 }
134
135 void ArchiveWidget::slotArchivingFinished(KJob *job)
136 {
137     progressBar->setValue(100);
138     buttonBox->button(QDialogButtonBox::Apply)->setText(i18n("Archive"));
139     if (job->error() == 0) text_info->setText(i18n("Project was successfully archived."));
140     else {
141         icon_info->setPixmap(KIcon("dialog-close").pixmap(16, 16));
142         text_info->setText(i18n("There was an error while copying the files: %1", job->errorString()));
143     }
144     m_copyJob = NULL;
145 }
146
147 void ArchiveWidget::slotArchivingProgress(KJob *, qulonglong size)
148 {
149     progressBar->setValue((int) 100 * size / m_requestedSize);
150 }
151