1
/***************************************************************************
2
smb4kmounthelper - The helper that mounts and unmounts shares.
5
copyright : (C) 2010 by Alexander Reinholdt
6
email : alexander.reinholdt@kdemail.net
7
***************************************************************************/
9
/***************************************************************************
10
* This program is free software; you can redistribute it and/or modify *
11
* it under the terms of the GNU General Public License as published by *
12
* the Free Software Foundation; either version 2 of the License, or *
13
* (at your option) any later version. *
15
* This program is distributed in the hope that it will be useful, but *
16
* WITHOUT ANY WARRANTY; without even the implied warranty of *
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
18
* General Public License for more details. *
20
* You should have received a copy of the GNU General Public License *
21
* along with this program; if not, write to the *
22
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
24
***************************************************************************/
27
#include <QProcessEnvironment>
33
#include <kstandarddirs.h>
36
#include <kmountpoint.h>
38
// application specific includes
39
#include "smb4kmounthelper.h"
41
KDE4_AUTH_HELPER_MAIN( "de.berlios.smb4k.mounthelper", Smb4KMountHelper )
44
ActionReply Smb4KMountHelper::mount( const QVariantMap &args )
47
reply.addData( "share_url", args["share_url"] );
48
reply.addData( "share_workgroup", args["share_workgroup"] );
49
reply.addData( "share_comment", args["share_comment"] );
50
reply.addData( "share_ip", args["share_ip"] );
51
reply.addData( "share_mountpoint", args["share_mountpoint"] );
52
reply.addData( "key", args["key"] );
54
KProcess proc( this );
55
proc.setOutputChannelMode( KProcess::SeparateChannels );
56
proc.setProcessEnvironment( QProcessEnvironment::systemEnvironment() );
57
proc.setEnv( "PASSWD", args["share_url"].toUrl().password(), true );
58
// The $HOME environment variable is needed under FreeBSD to
59
// point the mount process to the right ~/.nsmbrc file. Under
60
// Linux it is not needed.
61
proc.setEnv( "HOME", args["home_dir"].toString() );
62
proc.setProgram( args["mount_command"].toStringList() );
64
// Run the mount process.
67
if ( proc.waitForStarted( -1 ) )
69
// We want to be able to terminate the process from outside.
70
// Thus, we implement a loop that checks periodically, if we
71
// need to kill the process.
72
bool user_kill = false;
74
while ( !proc.waitForFinished( 10 ) )
76
if ( HelperSupport::isStopped() )
88
if ( proc.exitStatus() == KProcess::CrashExit )
92
reply.setErrorCode( ActionReply::HelperError );
93
reply.setErrorDescription( i18n( "The mount process crashed." ) );
103
// Check if there is output on stderr.
104
QString stderr = QString::fromUtf8( proc.readAllStandardError() );
105
reply.addData( "stderr", stderr );
107
// Check if there is output on stdout.
108
QString stdout = QString::fromUtf8( proc.readAllStandardOutput() );
109
reply.addData( "stdout", stdout );
114
reply.setErrorCode( ActionReply::HelperError );
115
reply.setErrorDescription( i18n( "The mount process could not be started." ) );
124
ActionReply Smb4KMountHelper::unmount( const QVariantMap &args )
127
reply.addData( "share_url", args["share_url"] );
128
reply.addData( "share_mountpoint", args["share_mountpoint"] );
129
reply.addData( "key", args["key"] );
131
// Check if the mountpoint is valid and the file system is
133
bool mountpoint_ok = false;
134
KMountPoint::List mountpoints = KMountPoint::currentMountPoints( KMountPoint::BasicInfoNeeded|KMountPoint::NeedMountOptions );
136
for( int j = 0; j < mountpoints.size(); j++ )
139
if ( QString::compare( args["share_mountpoint"].toString(),
140
mountpoints.at( j )->mountPoint(), Qt::CaseInsensitive ) == 0 &&
141
QString::compare( mountpoints.at( j )->mountType(), "cifs", Qt::CaseInsensitive ) == 0 )
143
if ( QString::compare( args["share_mountpoint"].toString(),
144
mountpoints.at( j )->mountPoint(), Qt::CaseInsensitive ) == 0 &&
145
QString::compare( mountpoints.at( j )->mountType(), "smbfs", Qt::CaseInsensitive ) == 0 )
148
mountpoint_ok = true;
157
if ( !mountpoint_ok )
159
reply.setErrorCode( ActionReply::HelperError );
160
reply.setErrorDescription( i18n( "The mountpoint is invalid." ) );
168
KProcess proc( this );
169
proc.setOutputChannelMode( KProcess::SeparateChannels );
170
proc.setProcessEnvironment( QProcessEnvironment::systemEnvironment() );
171
proc.setProgram( args["umount_command"].toStringList() );
173
// Run the unmount process.
176
if ( proc.waitForStarted( -1 ) )
178
// We want to be able to terminate the process from outside.
179
// Thus, we implement a loop that checks periodically, if we
180
// need to kill the process.
181
bool user_kill = false;
183
while ( !proc.waitForFinished( 10 ) )
185
if ( HelperSupport::isStopped() )
197
if ( proc.exitStatus() == KProcess::CrashExit )
201
reply.setErrorCode( ActionReply::HelperError );
202
reply.setErrorDescription( i18n( "The unmount process crashed." ) );
212
// Check if there is output on stderr.
213
QString stderr = QString::fromUtf8( proc.readAllStandardError() );
214
reply.addData( "stderr", stderr );
216
// Check if there is output on stdout.
217
QString stdout = QString::fromUtf8( proc.readAllStandardOutput() );
218
reply.addData( "stdout", stdout );
223
reply.setErrorCode( ActionReply::HelperError );
224
reply.setErrorDescription( i18n( "The unmount process could not be started." ) );
231
#include "smb4kmounthelper.moc"