summaryrefslogtreecommitdiffstats
path: root/kio/kio/kautomount.cpp
blob: e7df61a0aac1943142520284bfbf5314069d304b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* This file is part of the KDE libraries
   Copyright (C) 2000 David Faure <faure@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "kautomount.h"
#include "krun.h"
#include "kdirwatch.h"
#include "kio/job.h"
#include <kdirnotify_stub.h>
#include <kdebug.h>

/***********************************************************************
 *
 * Utility classes
 *
 ***********************************************************************/

KAutoMount::KAutoMount( bool _readonly, const TQString& _format, const TQString& _device,
                        const TQString&  _mountpoint, const TQString & _desktopFile,
                        bool _show_filemanager_window )
  : m_strDevice( _device ),
    m_desktopFile( _desktopFile )
{
  //kdDebug(7015) << "KAutoMount device=" << _device << " mountpoint=" << _mountpoint << endl;
  m_bShowFilemanagerWindow = _show_filemanager_window;

  KIO::Job* job = KIO::mount( _readonly, _format.ascii(), _device, _mountpoint );
  connect( job, TQT_SIGNAL( result( KIO::Job * ) ), this, TQT_SLOT( slotResult( KIO::Job * ) ) );
}

void KAutoMount::slotResult( KIO::Job * job )
{
  if ( job->error() ) {
    emit error();
    job->showErrorDialog();
  }
  else
  {
    KURL mountpoint;
    mountpoint.setPath( KIO::tqfindDeviceMountPoint( m_strDevice ) );
    //kdDebug(7015) << "KAutoMount: m_strDevice=" << m_strDevice << " -> mountpoint=" << mountpoint << endl;
    Q_ASSERT( mountpoint.isValid() );

    if ( mountpoint.path().isEmpty() )
        kdWarning(7015) << m_strDevice << " was correctly mounted, but KIO::tqfindDeviceMountPoint didn't tqfind it. "
                        << "This looks like a bug, please report it on http://bugs.kde.org, together with your /etc/fstab line" << endl;
    else if ( m_bShowFilemanagerWindow )
      KRun::runURL( mountpoint, "inode/directory" );

    // Notify about the new stuff in that dir, in case of opened windows showing it
    KDirNotify_stub allDirNotify("*", "KDirNotify*");
    allDirNotify.FilesAdded( mountpoint );

    // Update the desktop file which is used for mount/unmount (icon change)
    kdDebug(7015) << " mount finished : updating " << m_desktopFile << endl;
    KURL dfURL;
    dfURL.setPath( m_desktopFile );
    allDirNotify.FilesChanged( dfURL );
    //KDirWatch::self()->setFileDirty( m_desktopFile );

    emit finished();
  }
  delete this;
}

KAutoUnmount::KAutoUnmount( const TQString & _mountpoint, const TQString & _desktopFile )
  : m_desktopFile( _desktopFile ), m_mountpoint( _mountpoint )
{
  KIO::Job * job = KIO::unmount( m_mountpoint );
  connect( job, TQT_SIGNAL( result( KIO::Job * ) ), this, TQT_SLOT( slotResult( KIO::Job * ) ) );
}

void KAutoUnmount::slotResult( KIO::Job * job )
{
  if ( job->error() ) {
    emit error();
    job->showErrorDialog();
  }
  else
  {
    KDirNotify_stub allDirNotify("*", "KDirNotify*");
    // Update the desktop file which is used for mount/unmount (icon change)
    kdDebug(7015) << "unmount finished : updating " << m_desktopFile << endl;
    KURL dfURL;
    dfURL.setPath( m_desktopFile );
    allDirNotify.FilesChanged( dfURL );
    //KDirWatch::self()->setFileDirty( m_desktopFile );

    // Notify about the new stuff in that dir, in case of opened windows showing it
    // You may think we removed files, but this may have also readded some
    // (if the mountpoint wasn't empty). The only possible behavior on FilesAdded
    // is to relist the directory anyway.
    KURL mp;
    mp.setPath( m_mountpoint );
    allDirNotify.FilesAdded( mp );

    emit finished();
  }

  delete this;
}

#include "kautomount.moc"