Backport r950340 | aacid | 2009-04-06 23:21:18 +0200 (Mon, 06 Apr 2009) | 4 lines
[kdepim.git] / kmail / quotajobs.cpp
blobf77b151d2fa87c9c3cadc3589dc4c21ae8ec3c0f
1 /**
2 * quotajobs.cpp
4 * Copyright (c) 2006 Till Adam <adam@kde.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give
21 * permission to link the code of this program with any edition of
22 * the Qt library by Trolltech AS, Norway (or with modified versions
23 * of Qt that use the same license as Qt), and distribute linked
24 * combinations including the two. You must obey the GNU General
25 * Public License in all respects for all of the code used other than
26 * Qt. If you modify this file, you may extend this exception to
27 * your version of the file, but you are not obligated to do so. If
28 * you do not wish to do so, delete this exception statement from
29 * your version.
31 #include "quotajobs.h"
32 #include <kio/scheduler.h>
33 #include <kdebug.h>
35 using namespace KMail;
37 QuotaJobs::GetQuotarootJob* QuotaJobs::getQuotaroot(
38 KIO::Slave* slave, const KUrl& url )
40 QByteArray packedArgs;
41 QDataStream stream( &packedArgs, IO_WriteOnly );
42 stream << (int)'Q' << (int)'R' << url;
44 GetQuotarootJob* job = new GetQuotarootJob( url, packedArgs);
45 KIO::Scheduler::assignJobToSlave( slave, job );
46 return job;
49 QuotaJobs::GetQuotarootJob::GetQuotarootJob( const KUrl& url,
50 const QByteArray &packedArgs)
51 : KIO::SpecialJob( url, packedArgs)
53 connect( this, SIGNAL(infoMessage(KJob*,const QString&,const QString&)),
54 SLOT(slotInfoMessage(KJob*,const QString&,const QString&)) );
57 void QuotaJobs::GetQuotarootJob::slotInfoMessage( KJob*, const QString& str,
58 const QString& )
60 // Parse the result
61 QStringList results = str.split('\r');
62 QStringList roots;
63 QuotaInfoList quotas;
64 if ( results.size() > 0 ) {
65 // the first line is the available roots
66 roots = results.front().split( ' ' );
67 results.pop_front();
68 // the rest are pairs of root -> list of triplets
69 while ( results.size() > 0 ) {
70 QString root = results.front(); results.pop_front();
71 // and the quotas
72 if ( results.size() > 0 ) {
73 QStringList triplets = results.front().split( ' ' );
74 results.pop_front();
75 while ( triplets.size() >= 3 ) {
76 // there's always three, the label, current and max values
77 QString name = triplets.front(); triplets.pop_front();
78 QString current = triplets.front(); triplets.pop_front();
79 QString max = triplets.front(); triplets.pop_front();
80 QuotaInfo info( name, root, current, max );
81 quotas.append( info );
86 if ( !quotas.isEmpty() ) {
87 emit quotaInfoReceived( quotas );
89 emit quotaRootResult( roots );
92 QuotaJobs::GetStorageQuotaJob* QuotaJobs::getStorageQuota(
93 KIO::Slave* slave, const KUrl& url )
95 GetStorageQuotaJob* job = new GetStorageQuotaJob( slave, url );
96 return job;
100 QuotaJobs::GetStorageQuotaJob::GetStorageQuotaJob( KIO::Slave* slave, const KUrl& url )
101 : KIO::Job()
103 QByteArray packedArgs;
104 QDataStream stream( &packedArgs, IO_WriteOnly );
105 stream << (int)'Q' << (int)'R' << url;
107 QuotaJobs::GetQuotarootJob *job =
108 new QuotaJobs::GetQuotarootJob( url, packedArgs);
109 connect(job, SIGNAL(quotaInfoReceived(const QuotaInfoList&)),
110 SLOT(slotQuotaInfoReceived(const QuotaInfoList&)));
111 connect(job, SIGNAL(quotaRootResult(const QStringList&)),
112 SLOT(slotQuotarootResult(const QStringList&)));
113 KIO::Scheduler::assignJobToSlave( slave, job );
114 addSubjob( job );
117 void QuotaJobs::GetStorageQuotaJob::slotQuotarootResult( const QStringList& roots )
119 Q_UNUSED(roots); // we only support one for now
120 if ( !mStorageQuotaInfo.isValid() && !error() ) {
121 // No error, so the account supports quota, but no usable info
122 // was transmitted => no quota set on the folder. Make the info
123 // valid, bit leave it empty.
124 mStorageQuotaInfo.setName( "STORAGE" );
126 if ( mStorageQuotaInfo.isValid() )
127 emit storageQuotaResult( mStorageQuotaInfo );
128 // Our subjob is done for all we care although it technically isn't yet (we would need to wait
129 // for its finished() signal), however our dtor will cancel all still running subjobs which kills
130 // the slave, so detach our subjob and let it cleanup itself.
131 clearSubjobs();
132 emitResult();
135 void QuotaJobs::GetStorageQuotaJob::slotQuotaInfoReceived( const QuotaInfoList& infos )
137 QuotaInfoList::ConstIterator it( infos.begin() );
138 while ( it != infos.end() ) {
139 // FIXME we only use the first storage quota, for now
140 if ( it->name() == "STORAGE" && !mStorageQuotaInfo.isValid() ) {
141 mStorageQuotaInfo = *it;
143 ++it;
147 QuotaInfo QuotaJobs::GetStorageQuotaJob::storageQuotaInfo() const
149 return mStorageQuotaInfo;
152 #include "quotajobs.moc"