~ubuntu-branches/ubuntu/karmic/choqok/karmic

« back to all changes in this revision

Viewing changes to src/mediamanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve Stalcup
  • Date: 2009-02-11 20:31:26 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090211203126-7uqucqt5r2harpds
Tags: 0.4-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
    This file is part of choqoK, the KDE Twitter client
3
 
 
4
 
    Copyright (C) 2008 Mehrdad Momeny <mehrdad.momeny@gmail.com>
5
 
 
6
 
    This program is free software; you can redistribute it and/or modify
7
 
    it under the terms of the GNU General Public License as published by
8
 
    the Free Software Foundation; either version 3 of the License, or
9
 
    (at your option) any later version.
 
2
    This file is part of choqoK, the KDE mono-blogging client
 
3
 
 
4
    Copyright (C) 2008-2009 Mehrdad Momeny <mehrdad.momeny@gmail.com>
 
5
 
 
6
    This program is free software; you can redistribute it and/or
 
7
    modify it under the terms of the GNU General Public License as
 
8
    published by the Free Software Foundation; either version 2 of
 
9
    the License or (at your option) version 3 or any later version
 
10
    accepted by the membership of KDE e.V. (or its successor approved
 
11
    by the membership of KDE e.V.), which shall act as a proxy
 
12
    defined in Section 14 of version 3 of the license.
 
13
 
10
14
 
11
15
    This program is distributed in the hope that it will be useful,
12
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
18
    GNU General Public License for more details.
15
19
 
16
20
    You should have received a copy of the GNU General Public License
17
 
    along with this program; if not, write to the Free Software
18
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
21
    along with this program; if not, see http://www.gnu.org/licenses/
19
22
 
20
23
*/
21
24
#include "mediamanager.h"
29
32
#include <kdebug.h>
30
33
#include <KDE/KLocale>
31
34
 
32
 
MediaManager::MediaManager(QObject* parent): QObject(parent)
 
35
MediaManager::MediaManager( QObject* parent ): QObject( parent )
33
36
{
34
 
        kDebug();
35
 
        mediaResource = new KConfig();
36
 
        map = new KConfigGroup(mediaResource, "MediaMap");
 
37
    kDebug();
 
38
    mediaResource = new KConfig();
 
39
    map = new KConfigGroup( mediaResource, "MediaMap" );
37
40
}
38
41
 
39
42
 
40
43
MediaManager::~MediaManager()
41
44
{
42
 
        kDebug();
 
45
    kDebug();
43
46
    mSelf = 0L;
44
 
        map->sync();
45
 
        delete map;
46
 
        delete mediaResource;
 
47
    map->sync();
 
48
    delete map;
 
49
    delete mediaResource;
47
50
}
48
51
 
49
52
MediaManager * MediaManager::mSelf = 0L;
50
53
 
51
54
MediaManager * MediaManager::self()
52
55
{
53
 
    if(!mSelf)
 
56
    if ( !mSelf )
54
57
        mSelf = new MediaManager;
55
58
    return mSelf;
56
59
}
57
60
 
58
 
QString MediaManager::getImageLocalPathIfExist(const QString & remotePath)
 
61
QString MediaManager::getImageLocalPathIfExist( const QString & remotePath )
59
62
{
60
 
        QString path = map->readEntry(remotePath, QString(' '));
61
 
        return path;
 
63
    QString path = map->readEntry( remotePath, QString( ' ' ) );
 
64
    return path;
62
65
}
63
66
 
64
 
void MediaManager::getImageLocalPathDownloadAsyncIfNotExists(const QString & localName, const QString & remotePath)
 
67
void MediaManager::getImageLocalPathDownloadAsyncIfNotExists( const QString & localName, const QString & remotePath )
65
68
{
66
69
//     kDebug();
67
 
    if(mMediaFilesMap.contains(remotePath)){
 
70
    if ( mMediaFilesMap.contains( remotePath ) ) {
68
71
        ///The file is on the way, wait to download complete.
69
72
        return;
70
73
    }
71
74
    QString local;
72
 
    if(map->hasKey(remotePath)){
73
 
        local = map->readEntry(remotePath, QString());
74
 
        emit imageFetched(remotePath, local);
 
75
    if ( map->hasKey( remotePath ) ) {
 
76
        local = map->readEntry( remotePath, QString() );
 
77
        emit imageFetched( remotePath, local );
75
78
    } else {
76
 
        local = MEDIA_DIR+'/'+localName;
 
79
        local = MEDIA_DIR + '/' + localName;
77
80
        mMediaFilesMap [ remotePath ] = local;
78
 
        KUrl srcUrl(remotePath);
79
 
        KUrl destUrl(local);
 
81
        KUrl srcUrl( remotePath );
 
82
        KUrl destUrl( local );
80
83
 
81
 
        KIO::FileCopyJob *job = KIO::file_copy(srcUrl, destUrl, -1, KIO::HideProgressInfo | KIO::Overwrite) ;
82
 
        if(!job){
83
 
            kDebug()<<"Cannot create a FileCopyJob!";
84
 
            QString errMsg = i18n("Cannot download userimage for %1, please check your internet connection.", localName);
85
 
            emit sigError(errMsg);
 
84
        KIO::FileCopyJob *job = KIO::file_copy( srcUrl, destUrl, -1, KIO::HideProgressInfo | KIO::Overwrite ) ;
 
85
        if ( !job ) {
 
86
            kDebug() << "Cannot create a FileCopyJob!";
 
87
            QString errMsg = i18n( "Cannot download userimage for %1, please check your internet connection.",
 
88
                                   localName );
 
89
            emit sigError( errMsg );
86
90
            return;
87
91
        }
88
 
        connect( job, SIGNAL(result(KJob*)), this, SLOT(slotImageFetched(KJob *)));
 
92
        connect( job, SIGNAL( result( KJob* ) ), this, SLOT( slotImageFetched( KJob * ) ) );
89
93
        job->start();
90
94
    }
91
95
}
92
96
 
93
 
void MediaManager::slotImageFetched(KJob * job)
 
97
void MediaManager::slotImageFetched( KJob * job )
94
98
{
95
99
//     kDebug();
96
 
    KIO::FileCopyJob *baseJob = qobject_cast<KIO::FileCopyJob *>(job);
97
 
        if(job->error()){
98
 
                kDebug()<<"Job error!"<<job->error()<<"\t"<<job->errorString();
99
 
        QString errMsg = i18n("Cannot download user image from %1. The returned result is: %2",
100
 
                              job->errorString(), baseJob->srcUrl().pathOrUrl());
101
 
                emit sigError(errMsg);
 
100
    KIO::FileCopyJob *baseJob = qobject_cast<KIO::FileCopyJob *>( job );
 
101
    if ( job->error() ) {
 
102
        kDebug() << "Job error!" << job->error() << "\t" << job->errorString();
 
103
        QString errMsg = i18n( "Cannot download user image from %1. The returned result is: %2",
 
104
                               job->errorString(), baseJob->srcUrl().pathOrUrl() );
 
105
        emit sigError( errMsg );
102
106
    } else {
103
107
        QString local = baseJob->destUrl().pathOrUrl();
104
 
        QString remote= baseJob->srcUrl().pathOrUrl();
105
 
        mMediaFilesMap.remove(remote);
106
 
                map->writeEntry(remote,  local);
107
 
                map->sync();
108
 
        emit imageFetched(remote, local);
109
 
        }
 
108
        QString remote = baseJob->srcUrl().pathOrUrl();
 
109
        mMediaFilesMap.remove( remote );
 
110
        map->writeEntry( remote,  local );
 
111
        map->sync();
 
112
        emit imageFetched( remote, local );
 
113
    }
110
114
}
111
115
 
112
116
#include "mediamanager.moc"