~ubuntu-branches/ubuntu/karmic/kdepim/karmic-backports

« back to all changes in this revision

Viewing changes to akonadi/tray/restore.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Mangold
  • Date: 2009-07-10 06:34:50 UTC
  • mfrom: (1.1.40 upstream)
  • Revision ID: james.westby@ubuntu.com-20090710063450-neojgew2fh0n3y0u
Tags: 4:4.2.96-0ubuntu1
* New upstream release
* Bump kde build-deps to 4.2.96

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of the KDE project
2
 
   Copyright (C) 2008 Omat Holding B.V. <info@omat.nl>
3
 
 
4
 
   This program is free software; you can redistribute it and/or
5
 
   modify it under the terms of the GNU General Public
6
 
   License as published by the Free Software Foundation; either
7
 
   version 2 of the License, or (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 GNU
12
 
   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 Free Software
16
 
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
 
*/
18
 
 
19
 
#include "restore.h"
20
 
#include "global.h"
21
 
 
22
 
#include <KDebug>
23
 
#include <KProcess>
24
 
#include <KStandardDirs>
25
 
#include <KTempDir>
26
 
#include <KUrl>
27
 
 
28
 
#include <QDir>
29
 
 
30
 
#include <kio/netaccess.h>
31
 
 
32
 
#include <akonadi/private/xdgbasedirs_p.h>
33
 
 
34
 
 
35
 
using namespace Akonadi;
36
 
 
37
 
/**
38
 
 * Use this class to restore a backup. possible() will tell you if all
39
 
 * apps needed for the restore are available. Don't proceed without them.
40
 
 * After that call restore() to get it running. Please make sure the parameter
41
 
 * has the tar.bz2 extension.
42
 
 */
43
 
Restore::Restore( QWidget *parent ) : QWidget( parent )
44
 
{
45
 
}
46
 
 
47
 
bool Restore::possible()
48
 
{
49
 
    const QString mysql = KStandardDirs::findExe( "mysql" );
50
 
    const QString bzip2 = KStandardDirs::findExe( "bzip2" );
51
 
    const QString tar = KStandardDirs::findExe( "tar" );
52
 
    kDebug() << "mysql:" << mysql << "bzip2:" << bzip2 << "tar:" << tar;
53
 
    return !mysql.isEmpty() && !bzip2.isEmpty() && !tar.isEmpty();
54
 
}
55
 
 
56
 
void Restore::restore( const KUrl& filename )
57
 
{
58
 
    if ( filename.isEmpty() ) {
59
 
        emit completed( false );
60
 
        return;
61
 
    }
62
 
 
63
 
    const QString sep = QDir::separator();
64
 
 
65
 
    /* first create the temp folder. */
66
 
    KTempDir *tempDir = new KTempDir( KStandardDirs::locateLocal( "tmp", "akonadi" ) );
67
 
    tempDir->setAutoRemove( false );
68
 
    kDebug() << "Temp dir: "<< tempDir->name();
69
 
 
70
 
    /* Extract the nice tar file. */
71
 
    KProcess *proc = new KProcess( this );
72
 
    QStringList params;
73
 
    params << "-C" << tempDir->name();
74
 
    params << "-xjf";
75
 
    params << filename.path();
76
 
    proc->setWorkingDirectory( tempDir->name() );
77
 
    proc->setProgram( KStandardDirs::findExe( "tar" ), params );
78
 
    int result = proc->execute();
79
 
    delete proc;
80
 
    if ( result != 0 ) {
81
 
        kWarning() << "Executed:" << KStandardDirs::findExe( "tar" ) << params << " Result: " << result;
82
 
        tempDir->unlink();
83
 
        delete tempDir;
84
 
        emit completed( false );
85
 
        return;
86
 
    }
87
 
 
88
 
    /* Copy over the KDE configuration files. */
89
 
    QDir dir( tempDir->name() + "kdeconfig" + sep );
90
 
    dir.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
91
 
    QFileInfoList list = dir.entryInfoList();
92
 
    for ( int i = 0; i < list.size(); ++i ) {
93
 
        QFileInfo fileInfo = list.at( i );
94
 
        const QString source = fileInfo.absoluteFilePath();
95
 
        const QString dest = KStandardDirs::locateLocal( "config", fileInfo.fileName() );
96
 
 
97
 
        kDebug() << "Restoring: " << source << "to:" << dest;
98
 
        KIO::NetAccess::file_copy( source, dest, this );
99
 
    }
100
 
 
101
 
    /* Copy over the Akonadi configuration files. */
102
 
    const QString akonadiconfigfolder = XdgBaseDirs::findResourceDir( "config", "akonadi" );
103
 
    dir.setPath( tempDir->name() + "akonadiconfig" + sep );
104
 
    dir.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
105
 
    list = dir.entryInfoList();
106
 
    for ( int i = 0; i < list.size(); ++i ) {
107
 
        QFileInfo fileInfo = list.at( i );
108
 
        const QString source = fileInfo.absoluteFilePath();
109
 
        const QString dest = akonadiconfigfolder + sep + fileInfo.fileName();
110
 
 
111
 
        kDebug() << "Restoring: " << source << "to:" << dest;
112
 
        KIO::NetAccess::file_copy( source, dest, this );
113
 
    }
114
 
 
115
 
    /* Restore the database */
116
 
    Tray::Global global;
117
 
    proc = new KProcess( this );
118
 
    params.clear();
119
 
    params << global.dboptions() << global.dbname();
120
 
    kDebug() << "Executing:" << KStandardDirs::findExe( "mysql" ) << params;
121
 
    proc->setStandardInputFile( tempDir->name() + "db" + sep + "database.sql" );
122
 
    proc->setProgram( KStandardDirs::findExe( "mysql" ), params );
123
 
    result = proc->execute();
124
 
    delete proc;
125
 
    if ( result != 0 ) {
126
 
        kWarning() << "Executed:" << KStandardDirs::findExe( "mysql" ) << params << " Result: " << result;
127
 
        tempDir->unlink();
128
 
        delete tempDir;
129
 
        emit completed( false );
130
 
        return;
131
 
    }
132
 
 
133
 
    tempDir->unlink();
134
 
    delete tempDir;
135
 
    emit completed( true );
136
 
}
137
 
 
138
 
#include "restore.moc"