fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kio / kio / kautomount.cpp
blobe8e6b056417ca45f60bfc11c71733fa904544544
1 /* This file is part of the KDE libraries
2 Copyright (C) 2000 David Faure <faure@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "kautomount.h"
20 #include "krun.h"
21 #include "kdirwatch.h"
22 #include "kio/job.h"
23 #include "kio/jobuidelegate.h"
24 #include <kdirnotify.h>
25 #include <kdebug.h>
26 #include <kmountpoint.h>
28 /***********************************************************************
30 * Utility classes
32 ***********************************************************************/
34 class KAutoMountPrivate
36 public:
37 KAutoMountPrivate(KAutoMount *qq, const QString &device, const QString &desktopFile,
38 bool showFileManagerWindow)
39 : q(qq), m_strDevice(device), m_desktopFile(desktopFile),
40 m_bShowFilemanagerWindow(showFileManagerWindow)
41 { }
43 KAutoMount *q;
44 QString m_strDevice;
45 QString m_desktopFile;
46 bool m_bShowFilemanagerWindow;
48 void slotResult( KJob * );
51 KAutoMount::KAutoMount( bool _readonly, const QByteArray& _format, const QString& _device,
52 const QString& _mountpoint, const QString & _desktopFile,
53 bool _show_filemanager_window )
54 : d(new KAutoMountPrivate(this, _device, _desktopFile, _show_filemanager_window))
56 KIO::Job* job = KIO::mount( _readonly, _format, _device, _mountpoint );
57 connect( job, SIGNAL( result( KJob * ) ), this, SLOT( slotResult( KJob * ) ) );
60 KAutoMount::~KAutoMount()
62 delete d;
65 void KAutoMountPrivate::slotResult( KJob * job )
67 if ( job->error() ) {
68 emit q->error();
69 job->uiDelegate()->showErrorMessage();
70 } else {
71 KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByDevice( m_strDevice );
72 if (!mp) {
73 kWarning(7015) << m_strDevice << "was correctly mounted, but findByDevice() didn't find it."
74 << "This looks like a bug, please report it on http://bugs.kde.org, together with your /etc/fstab and /etc/mtab lines for this device";
75 } else {
76 KUrl url(mp->mountPoint());
77 //kDebug(7015) << "KAutoMount: m_strDevice=" << m_strDevice << " -> mountpoint=" << mountpoint;
78 if ( m_bShowFilemanagerWindow ) {
79 KRun::runUrl( url, "inode/directory", 0 /*TODO - window*/ );
81 // Notify about the new stuff in that dir, in case of opened windows showing it
82 org::kde::KDirNotify::emitFilesAdded( url.url() );
85 // Update the desktop file which is used for mount/unmount (icon change)
86 kDebug(7015) << " mount finished : updating " << m_desktopFile;
87 KUrl dfURL;
88 dfURL.setPath( m_desktopFile );
89 org::kde::KDirNotify::emitFilesChanged( QStringList() << dfURL.url() );
90 //KDirWatch::self()->setFileDirty( m_desktopFile );
92 emit q->finished();
94 q->deleteLater();
97 class KAutoUnmountPrivate
99 public:
100 KAutoUnmountPrivate( KAutoUnmount *qq, const QString & _mountpoint, const QString & _desktopFile )
101 : q(qq), m_desktopFile( _desktopFile ), m_mountpoint( _mountpoint )
103 KAutoUnmount *q;
104 QString m_desktopFile;
105 QString m_mountpoint;
107 void slotResult( KJob * job );
110 KAutoUnmount::KAutoUnmount( const QString & _mountpoint, const QString & _desktopFile )
111 : d( new KAutoUnmountPrivate(this, _desktopFile, _mountpoint) )
113 KIO::Job * job = KIO::unmount( d->m_mountpoint );
114 connect( job, SIGNAL( result( KJob * ) ), this, SLOT( slotResult( KJob * ) ) );
117 void KAutoUnmountPrivate::slotResult( KJob * job )
119 if ( job->error() ) {
120 emit q->error();
121 job->uiDelegate()->showErrorMessage();
123 else
125 // Update the desktop file which is used for mount/unmount (icon change)
126 kDebug(7015) << "unmount finished : updating " << m_desktopFile;
127 KUrl dfURL;
128 dfURL.setPath( m_desktopFile );
129 org::kde::KDirNotify::emitFilesChanged( QStringList() << dfURL.url() );
130 //KDirWatch::self()->setFileDirty( m_desktopFile );
132 // Notify about the new stuff in that dir, in case of opened windows showing it
133 // You may think we removed files, but this may have also readded some
134 // (if the mountpoint wasn't empty). The only possible behavior on FilesAdded
135 // is to relist the directory anyway.
136 KUrl mp( m_mountpoint );
137 org::kde::KDirNotify::emitFilesAdded( mp.url() );
139 emit q->finished();
142 q->deleteLater();
145 KAutoUnmount::~KAutoUnmount()
147 delete d;
150 #include "kautomount.moc"