~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to app/saveallhelper.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2010 Aurélien Gâteau <agateau@kde.org>
38
38
#include <lib/document/documentfactory.h>
39
39
#include <lib/document/documentjob.h>
40
40
 
41
 
namespace Gwenview {
42
 
 
 
41
namespace Gwenview
 
42
{
43
43
 
44
44
struct SaveAllHelperPrivate {
45
 
        QWidget* mParent;
46
 
        KProgressDialog* mProgressDialog;
47
 
        QSet<DocumentJob*> mJobSet;
48
 
        QStringList mErrorList;
 
45
    QWidget* mParent;
 
46
    KProgressDialog* mProgressDialog;
 
47
    QSet<DocumentJob*> mJobSet;
 
48
    QStringList mErrorList;
49
49
};
50
50
 
51
 
 
52
51
SaveAllHelper::SaveAllHelper(QWidget* parent)
53
 
: d(new SaveAllHelperPrivate) {
54
 
        d->mParent = parent;
55
 
        d->mProgressDialog = new KProgressDialog(parent);
56
 
        connect(d->mProgressDialog, SIGNAL(cancelClicked()), SLOT(slotCanceled()));
57
 
        d->mProgressDialog->setLabelText(i18nc("@info:progress saving all image changes", "Saving..."));
58
 
        d->mProgressDialog->setButtonText(i18n("&Stop"));
59
 
        d->mProgressDialog->progressBar()->setMinimum(0);
60
 
}
61
 
 
62
 
 
63
 
SaveAllHelper::~SaveAllHelper() {
64
 
        delete d;
65
 
}
66
 
 
67
 
 
68
 
void SaveAllHelper::save() {
69
 
        KUrl::List list = DocumentFactory::instance()->modifiedDocumentList();
70
 
        d->mProgressDialog->progressBar()->setRange(0, list.size());
71
 
        d->mProgressDialog->progressBar()->setValue(0);
72
 
        Q_FOREACH(const KUrl& url, list) {
73
 
                Document::Ptr doc = DocumentFactory::instance()->load(url);
74
 
                DocumentJob* job = doc->save(url, doc->format());
75
 
                connect(job, SIGNAL(result(KJob*)), SLOT(slotResult(KJob*)));
76
 
                d->mJobSet << job;
77
 
        }
78
 
 
79
 
        d->mProgressDialog->exec();
80
 
 
81
 
        // Done, show message if necessary
82
 
        if (d->mErrorList.count() > 0) {
83
 
                QString msg = i18ncp("@info", "One document could not be saved:", "%1 documents could not be saved:", d->mErrorList.count());
84
 
                msg += "<ul>";
85
 
                Q_FOREACH(const QString& item, d->mErrorList) {
86
 
                        msg += "<li>" + item + "</li>";
87
 
                }
88
 
                msg += "</ul>";
89
 
                KMessageBox::sorry(d->mParent, msg);
90
 
        }
91
 
}
92
 
 
93
 
 
94
 
void SaveAllHelper::slotCanceled() {
95
 
        Q_FOREACH(DocumentJob* job, d->mJobSet) {
96
 
                job->kill();
97
 
        }
98
 
}
99
 
 
100
 
 
101
 
void SaveAllHelper::slotResult(KJob* _job) {
102
 
        DocumentJob* job = static_cast<DocumentJob*>(_job);
103
 
        if (job->error()) {
104
 
                KUrl url = job->document()->url();
105
 
                QString name = url.fileName().isEmpty() ? url.pathOrUrl() : url.fileName();
106
 
                d->mErrorList << i18nc("@info %1 is the name of the document which failed to save, %2 is the reason for the failure",
107
 
                        "<filename>%1</filename>: %2", name, job->errorString());
108
 
        }
109
 
        d->mJobSet.remove(job);
110
 
        QProgressBar* bar = d->mProgressDialog->progressBar();
111
 
        bar->setValue(bar->value() + 1);
112
 
}
113
 
 
114
 
 
 
52
: d(new SaveAllHelperPrivate)
 
53
{
 
54
    d->mParent = parent;
 
55
    d->mProgressDialog = new KProgressDialog(parent);
 
56
    connect(d->mProgressDialog, SIGNAL(cancelClicked()), SLOT(slotCanceled()));
 
57
    d->mProgressDialog->setLabelText(i18nc("@info:progress saving all image changes", "Saving..."));
 
58
    d->mProgressDialog->setButtonText(i18n("&Stop"));
 
59
    d->mProgressDialog->progressBar()->setMinimum(0);
 
60
}
 
61
 
 
62
SaveAllHelper::~SaveAllHelper()
 
63
{
 
64
    delete d;
 
65
}
 
66
 
 
67
void SaveAllHelper::save()
 
68
{
 
69
    KUrl::List list = DocumentFactory::instance()->modifiedDocumentList();
 
70
    d->mProgressDialog->progressBar()->setRange(0, list.size());
 
71
    d->mProgressDialog->progressBar()->setValue(0);
 
72
    Q_FOREACH(const KUrl & url, list) {
 
73
        Document::Ptr doc = DocumentFactory::instance()->load(url);
 
74
        DocumentJob* job = doc->save(url, doc->format());
 
75
        connect(job, SIGNAL(result(KJob*)), SLOT(slotResult(KJob*)));
 
76
        d->mJobSet << job;
 
77
    }
 
78
 
 
79
    d->mProgressDialog->exec();
 
80
 
 
81
    // Done, show message if necessary
 
82
    if (d->mErrorList.count() > 0) {
 
83
        QString msg = i18ncp("@info", "One document could not be saved:", "%1 documents could not be saved:", d->mErrorList.count());
 
84
        msg += "<ul>";
 
85
        Q_FOREACH(const QString & item, d->mErrorList) {
 
86
            msg += "<li>" + item + "</li>";
 
87
        }
 
88
        msg += "</ul>";
 
89
        KMessageBox::sorry(d->mParent, msg);
 
90
    }
 
91
}
 
92
 
 
93
void SaveAllHelper::slotCanceled()
 
94
{
 
95
    Q_FOREACH(DocumentJob * job, d->mJobSet) {
 
96
        job->kill();
 
97
    }
 
98
}
 
99
 
 
100
void SaveAllHelper::slotResult(KJob* _job)
 
101
{
 
102
    DocumentJob* job = static_cast<DocumentJob*>(_job);
 
103
    if (job->error()) {
 
104
        KUrl url = job->document()->url();
 
105
        QString name = url.fileName().isEmpty() ? url.pathOrUrl() : url.fileName();
 
106
        d->mErrorList << i18nc("@info %1 is the name of the document which failed to save, %2 is the reason for the failure",
 
107
                               "<filename>%1</filename>: %2", name, job->errorString());
 
108
    }
 
109
    d->mJobSet.remove(job);
 
110
    QProgressBar* bar = d->mProgressDialog->progressBar();
 
111
    bar->setValue(bar->value() + 1);
 
112
}
115
113
 
116
114
} // namespace