~ubuntu-branches/ubuntu/lucid/kdebase/lucid

« back to all changes in this revision

Viewing changes to kcontrol/konq/rootopts.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ana Beatriz Guerrero Lopez
  • Date: 2009-04-05 05:22:13 UTC
  • mfrom: (0.4.2 experimental) (0.2.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 235.
  • Revision ID: james.westby@ubuntu.com-20090405052213-39thr4l6p2ss07uj
Tags: 4:4.2.2-1
* New upstream release:
  - khtml fixes. (Closes: #290285, #359680)
  - Default konsole sessions can be deleted. (Closes: #286342)
  - Tag widget uses standard application palette. (Closes: #444800)
  - ... and surely many more but we have lost track...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
//
3
 
// "Desktop Options" Tab for KDesktop configuration
4
 
//
5
 
// (c) Martin R. Jones 1996
6
 
// (c) Bernd Wuebben 1998
7
 
//
8
 
// Layouts
9
 
// (c) Christian Tibirna 1998
10
 
// Port to KControl, split from Misc Tab, Port to KControl2
11
 
// (c) David Faure 1998
12
 
// Desktop menus, paths
13
 
// (c) David Faure 2000
14
 
 
15
 
#include <config.h>
16
 
 
17
 
#include <qcheckbox.h>
18
 
#include <qcombobox.h>
19
 
#include <qlabel.h>
20
 
#include <qlayout.h>
21
 
#include <qvgroupbox.h>
22
 
#include <qwhatsthis.h>
23
 
 
24
 
#include <dcopclient.h>
25
 
 
26
 
#include <kapplication.h>
27
 
#include <kcustommenueditor.h>
28
 
#include <kdebug.h>
29
 
#include <kfileitem.h>
30
 
#include <kglobalsettings.h>
31
 
#include <kipc.h>
32
 
#include <klistview.h>
33
 
#include <kio/job.h>
34
 
#include <klocale.h>
35
 
#include <kmessagebox.h>
36
 
#include <kmimetype.h>
37
 
#include <kstandarddirs.h>
38
 
#include <ktrader.h>
39
 
#include <konq_defaults.h> // include default values directly from libkonq
40
 
#include <kurlrequester.h>
41
 
 
42
 
#include "rootopts.h"
43
 
 
44
 
//-----------------------------------------------------------------------------
45
 
 
46
 
DesktopPathConfig::DesktopPathConfig(QWidget *parent, const char * )
47
 
    : KCModule( parent, "kcmkonq" )
48
 
{
49
 
  QLabel * tmpLabel;
50
 
 
51
 
#undef RO_LASTROW
52
 
#undef RO_LASTCOL
53
 
#define RO_LASTROW 4   // 3 paths + last row
54
 
#define RO_LASTCOL 2
55
 
 
56
 
  int row = 0;
57
 
  QGridLayout *lay = new QGridLayout(this, RO_LASTROW+1, RO_LASTCOL+1,
58
 
      0, KDialog::spacingHint());
59
 
 
60
 
  lay->setRowStretch(RO_LASTROW,10); // last line grows
61
 
 
62
 
  lay->setColStretch(0,0);
63
 
  lay->setColStretch(1,0);
64
 
  lay->setColStretch(2,10);
65
 
 
66
 
 
67
 
  setQuickHelp( i18n("<h1>Paths</h1>\n"
68
 
    "This module allows you to choose where in the filesystem the "
69
 
    "files on your desktop should be stored.\n"
70
 
    "Use the \"What's This?\" (Shift+F1) to get help on specific options."));
71
 
 
72
 
  // Desktop Paths
73
 
  row++;
74
 
  tmpLabel = new QLabel(i18n("Des&ktop path:"), this);
75
 
  lay->addWidget(tmpLabel, row, 0);
76
 
  urDesktop = new KURLRequester(this);
77
 
  urDesktop->setMode( KFile::Directory );
78
 
  tmpLabel->setBuddy( urDesktop );
79
 
  lay->addMultiCellWidget(urDesktop, row, row, 1, RO_LASTCOL);
80
 
  connect(urDesktop, SIGNAL(textChanged(const QString &)), this, SLOT(changed()));
81
 
  QString wtstr = i18n("This folder contains all the files"
82
 
                       " which you see on your desktop. You can change the location of this"
83
 
                       " folder if you want to, and the contents will move automatically"
84
 
                       " to the new location as well.");
85
 
  QWhatsThis::add( tmpLabel, wtstr );
86
 
  QWhatsThis::add( urDesktop, wtstr );
87
 
 
88
 
  row++;
89
 
  tmpLabel = new QLabel(i18n("A&utostart path:"), this);
90
 
  lay->addWidget(tmpLabel, row, 0);
91
 
  urAutostart = new KURLRequester(this);
92
 
  urAutostart->setMode( KFile::Directory );
93
 
  tmpLabel->setBuddy( urAutostart );
94
 
  lay->addMultiCellWidget(urAutostart, row, row, 1, RO_LASTCOL);
95
 
  connect(urAutostart, SIGNAL(textChanged(const QString &)), this, SLOT(changed()));
96
 
  wtstr = i18n("This folder contains applications or"
97
 
               " links to applications (shortcuts) that you want to have started"
98
 
               " automatically whenever KDE starts. You can change the location of this"
99
 
               " folder if you want to, and the contents will move automatically"
100
 
               " to the new location as well.");
101
 
  QWhatsThis::add( tmpLabel, wtstr );
102
 
  QWhatsThis::add( urAutostart, wtstr );
103
 
 
104
 
  row++;
105
 
  tmpLabel = new QLabel(i18n("D&ocuments path:"), this);
106
 
  lay->addWidget(tmpLabel, row, 0);
107
 
  urDocument = new KURLRequester(this);
108
 
  urDocument->setMode( KFile::Directory );
109
 
  tmpLabel->setBuddy( urDocument );
110
 
  lay->addMultiCellWidget(urDocument, row, row, 1, RO_LASTCOL);
111
 
  connect(urDocument, SIGNAL(textChanged(const QString &)), this, SLOT(changed()));
112
 
  wtstr = i18n("This folder will be used by default to "
113
 
               "load or save documents from or to.");
114
 
  QWhatsThis::add( tmpLabel, wtstr );
115
 
  QWhatsThis::add( urDocument, wtstr );
116
 
 
117
 
  // -- Bottom --
118
 
  Q_ASSERT( row == RO_LASTROW-1 ); // if it fails here, check the row++ and RO_LASTROW above
119
 
 
120
 
  load();
121
 
}
122
 
 
123
 
void DesktopPathConfig::load()
124
 
{
125
 
        load( false );
126
 
}
127
 
 
128
 
void DesktopPathConfig::load( bool useDefaults )
129
 
{
130
 
    KConfig config("kdeglobals", true, false);
131
 
    // Desktop Paths
132
 
         config.setReadDefaults( useDefaults );
133
 
 
134
 
         config.setGroup("Paths");
135
 
    urDesktop->setURL( config.readPathEntry( "Desktop" , KGlobalSettings::desktopPath() ));
136
 
    urAutostart->setURL( config.readPathEntry( "Autostart" , KGlobalSettings::autostartPath() ));
137
 
    urDocument->setURL( config.readPathEntry( "Documents", KGlobalSettings::documentPath() ));
138
 
 
139
 
         emit changed( useDefaults );
140
 
}
141
 
 
142
 
void DesktopPathConfig::defaults()
143
 
{
144
 
        load( true );
145
 
}
146
 
 
147
 
void DesktopPathConfig::save()
148
 
{
149
 
    KConfig *config = KGlobal::config();
150
 
    KConfigGroupSaver cgs( config, "Paths" );
151
 
 
152
 
    bool pathChanged = false;
153
 
    bool autostartMoved = false;
154
 
 
155
 
    KURL desktopURL;
156
 
    desktopURL.setPath( KGlobalSettings::desktopPath() );
157
 
    KURL newDesktopURL;
158
 
    newDesktopURL.setPath(urDesktop->url());
159
 
 
160
 
    KURL autostartURL;
161
 
    autostartURL.setPath( KGlobalSettings::autostartPath() );
162
 
    KURL newAutostartURL;
163
 
    newAutostartURL.setPath(urAutostart->url());
164
 
 
165
 
    KURL documentURL;
166
 
    documentURL.setPath( KGlobalSettings::documentPath() );
167
 
    KURL newDocumentURL;
168
 
    newDocumentURL.setPath(urDocument->url());
169
 
 
170
 
    if ( !newDesktopURL.equals( desktopURL, true ) )
171
 
    {
172
 
        // Test which other paths were inside this one (as it is by default)
173
 
        // and for each, test where it should go.
174
 
        // * Inside destination -> let them be moved with the desktop (but adjust name if necessary)
175
 
        // * Not inside destination -> move first
176
 
        // !!!
177
 
        kdDebug() << "desktopURL=" << desktopURL.url() << endl;
178
 
        QString urlDesktop = urDesktop->url();
179
 
        if ( !urlDesktop.endsWith( "/" ))
180
 
            urlDesktop+="/";
181
 
 
182
 
        if ( desktopURL.isParentOf( autostartURL ) )
183
 
        {
184
 
            kdDebug() << "Autostart is on the desktop" << endl;
185
 
 
186
 
            // Either the Autostart field wasn't changed (-> need to update it)
187
 
            if ( newAutostartURL.equals( autostartURL, true ) )
188
 
            {
189
 
                // Hack. It could be in a subdir inside desktop. Hmmm... Argl.
190
 
                urAutostart->setURL( urlDesktop + "Autostart/" );
191
 
                kdDebug() << "Autostart is moved with the desktop" << endl;
192
 
                autostartMoved = true;
193
 
            }
194
 
            // or it has been changed (->need to move it from here)
195
 
            else
196
 
            {
197
 
                KURL futureAutostartURL;
198
 
                futureAutostartURL.setPath( urlDesktop + "Autostart/" );
199
 
                if ( newAutostartURL.equals( futureAutostartURL, true ) )
200
 
                    autostartMoved = true;
201
 
                else
202
 
                    autostartMoved = moveDir( KURL( KGlobalSettings::autostartPath() ), KURL( urAutostart->url() ), i18n("Autostart") );
203
 
            }
204
 
        }
205
 
 
206
 
        if ( moveDir( KURL( KGlobalSettings::desktopPath() ), KURL( urlDesktop ), i18n("Desktop") ) )
207
 
        {
208
 
//            config->writeEntry( "Desktop", urDesktop->url());
209
 
            config->writePathEntry( "Desktop", urlDesktop, true, true );
210
 
            pathChanged = true;
211
 
        }
212
 
    }
213
 
 
214
 
    if ( !newAutostartURL.equals( autostartURL, true ) )
215
 
    {
216
 
        if (!autostartMoved)
217
 
            autostartMoved = moveDir( KURL( KGlobalSettings::autostartPath() ), KURL( urAutostart->url() ), i18n("Autostart") );
218
 
        if (autostartMoved)
219
 
        {
220
 
//            config->writeEntry( "Autostart", Autostart->url());
221
 
            config->writePathEntry( "Autostart", urAutostart->url(), true, true );
222
 
            pathChanged = true;
223
 
        }
224
 
    }
225
 
 
226
 
    if ( !newDocumentURL.equals( documentURL, true ) )
227
 
    {
228
 
        bool pathOk = true;
229
 
        QString path = urDocument->url();
230
 
        if (!QDir(path).exists())
231
 
        {
232
 
            if (!KStandardDirs::makeDir(path))
233
 
            {
234
 
                KMessageBox::sorry(this, KIO::buildErrorString(KIO::ERR_COULD_NOT_MKDIR, path));
235
 
                urDocument->setURL(documentURL.path());
236
 
                pathOk = false;
237
 
            }
238
 
        }
239
 
 
240
 
        if (pathOk)
241
 
        {
242
 
            config->writePathEntry( "Documents", path, true, true );
243
 
            pathChanged = true;
244
 
        }
245
 
    }
246
 
 
247
 
    config->sync();
248
 
 
249
 
    if (pathChanged)
250
 
    {
251
 
        kdDebug() << "DesktopPathConfig::save sending message SettingsChanged" << endl;
252
 
        KIPC::sendMessageAll(KIPC::SettingsChanged, KApplication::SETTINGS_PATHS);
253
 
    }
254
 
 
255
 
    // Tell kdesktop about the new config file
256
 
    if ( !kapp->dcopClient()->isAttached() )
257
 
       kapp->dcopClient()->attach();
258
 
    QByteArray data;
259
 
 
260
 
    int konq_screen_number = KApplication::desktop()->primaryScreen();
261
 
    QCString appname;
262
 
    if (konq_screen_number == 0)
263
 
        appname = "kdesktop";
264
 
    else
265
 
        appname.sprintf("kdesktop-screen-%d", konq_screen_number);
266
 
    kapp->dcopClient()->send( appname, "KDesktopIface", "configure()", data );
267
 
}
268
 
 
269
 
bool DesktopPathConfig::moveDir( const KURL & src, const KURL & dest, const QString & type )
270
 
{
271
 
    if (!src.isLocalFile() || !dest.isLocalFile())
272
 
        return true;
273
 
    m_ok = true;
274
 
    // Ask for confirmation before moving the files
275
 
    if ( KMessageBox::questionYesNo( this, i18n("The path for '%1' has been changed;\ndo you want the files to be moved from '%2' to '%3'?").
276
 
                              arg(type).arg(src.path()).arg(dest.path()), i18n("Confirmation Required"),i18n("Move"),KStdGuiItem::cancel() )
277
 
            == KMessageBox::Yes )
278
 
    {
279
 
        bool destExists = QFile::exists(dest.path());
280
 
        if (destExists)
281
 
        {
282
 
            m_copyToDest = dest;
283
 
            m_copyFromSrc = src;
284
 
            KIO::ListJob* job = KIO::listDir( src );
285
 
            connect( job, SIGNAL( entries( KIO::Job *, const KIO::UDSEntryList& ) ),
286
 
                     this, SLOT( slotEntries( KIO::Job *, const KIO::UDSEntryList& ) ) );
287
 
            qApp->enter_loop();
288
 
 
289
 
            if (m_ok)
290
 
            {
291
 
                KIO::del( src );
292
 
            }
293
 
        }
294
 
        else
295
 
        {
296
 
            KIO::Job * job = KIO::move( src, dest );
297
 
            connect( job, SIGNAL( result( KIO::Job * ) ), this, SLOT( slotResult( KIO::Job * ) ) );
298
 
            // wait for job
299
 
            qApp->enter_loop();
300
 
        }
301
 
    }
302
 
    kdDebug() << "DesktopPathConfig::slotResult returning " << m_ok << endl;
303
 
    return m_ok;
304
 
}
305
 
 
306
 
void DesktopPathConfig::slotEntries( KIO::Job * job, const KIO::UDSEntryList& list)
307
 
{
308
 
    if (job->error())
309
 
    {
310
 
        job->showErrorDialog(this);
311
 
        return;
312
 
    }
313
 
 
314
 
    KIO::UDSEntryListConstIterator it = list.begin();
315
 
    KIO::UDSEntryListConstIterator end = list.end();
316
 
    for (; it != end; ++it)
317
 
    {
318
 
        KFileItem file(*it, m_copyFromSrc, true, true);
319
 
        if (file.url() == m_copyFromSrc || file.url().fileName() == "..")
320
 
        {
321
 
            continue;
322
 
        }
323
 
 
324
 
        KIO::Job * moveJob = KIO::move( file.url(), m_copyToDest );
325
 
        connect( moveJob, SIGNAL( result( KIO::Job * ) ), this, SLOT( slotResult( KIO::Job * ) ) );
326
 
        qApp->enter_loop();
327
 
    }
328
 
    qApp->exit_loop();
329
 
}
330
 
 
331
 
void DesktopPathConfig::slotResult( KIO::Job * job )
332
 
{
333
 
    if (job->error())
334
 
    {
335
 
        if ( job->error() != KIO::ERR_DOES_NOT_EXIST )
336
 
            m_ok = false;
337
 
        // If the source doesn't exist, no wonder we couldn't move the dir.
338
 
        // In that case, trust the user and set the new setting in any case.
339
 
 
340
 
        job->showErrorDialog(this);
341
 
    }
342
 
    qApp->exit_loop();
343
 
}
344
 
 
345
 
#include "rootopts.moc"