when a new activity is added, don't move more than necessary to show it
[kdebase.git] / runtime / kioslave / smb / kio_smb_mount.cpp
blob4a097b126b9b793288acfbb0bc6046fcc35afeef
1 /* This file is part of the KDE project
3 Copyright (C) 2000 Alexander Neundorf <neundorf@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include "kio_smb.h"
22 #include <kstandarddirs.h>
23 #include <unistd.h>
24 #include <QByteArray>
25 #include <QDir>
26 #include <k3process.h>
27 #include <kshell.h>
28 void SMBSlave::readOutput(K3Process *, char *buffer, int buflen)
30 mybuf += QString::fromLocal8Bit(buffer, buflen);
33 void SMBSlave::readStdErr(K3Process *, char *buffer, int buflen)
35 mystderr += QString::fromLocal8Bit(buffer, buflen);
38 void SMBSlave::special( const QByteArray & data)
40 kDebug(KIO_SMB)<<"Smb::special()";
41 int tmp;
42 QDataStream stream(data);
43 stream >> tmp;
44 //mounting and umounting are both blocking, "guarded" by a SIGALARM in the future
45 switch (tmp)
47 case 1:
48 case 3:
50 QString remotePath, mountPoint, user;
51 stream >> remotePath >> mountPoint;
53 QStringList sl=remotePath.split( "/");
54 QString share,host;
55 if (sl.count()>=2)
57 host=sl.at(0).mid(2);
58 share=sl.at(1);
59 kDebug(KIO_SMB)<<"special() host -"<< host <<"- share -" << share <<"-";
62 remotePath.replace('\\', '/'); // smbmounterplugin sends \\host/share
64 kDebug(KIO_SMB) << "mounting: " << remotePath.toLocal8Bit() << " to " << mountPoint.toLocal8Bit();
66 if (tmp==3) {
67 if (!KStandardDirs::makeDir(mountPoint)) {
68 error(KIO::ERR_COULD_NOT_MKDIR, mountPoint);
69 return;
72 mybuf.truncate(0);
73 mystderr.truncate(0);
75 SMBUrl smburl(KUrl("smb:///"));
76 smburl.setHost(host);
77 smburl.setPath('/' + share);
79 if ( !checkPassword(smburl) )
81 finished();
82 return;
85 // using smbmount instead of "mount -t smbfs", because mount does not allow a non-root
86 // user to do a mount, but a suid smbmnt does allow this
88 K3Process proc;
89 proc.setUseShell(true); // to have the path to smbmnt (which is used by smbmount); see man smbmount
90 proc << "smbmount";
92 QString options;
94 if ( smburl.user().isEmpty() )
96 user = "guest";
97 options = "-o guest";
99 else
101 options = "-o username=" + KShell::quoteArg(smburl.user());
102 user = smburl.user();
104 if ( ! smburl.pass().isEmpty() )
105 options += ",password=" + KShell::quoteArg(smburl.pass());
108 // TODO: check why the control center uses encodings with a blank char, e.g. "cp 1250"
109 //if ( ! m_default_encoding.isEmpty() )
110 //options += ",codepage=" + KShell::quoteArg(m_default_encoding);
112 proc << KShell::quoteArg(remotePath.toLocal8Bit());
113 proc << KShell::quoteArg(mountPoint.toLocal8Bit());
114 proc << options;
116 connect(&proc, SIGNAL( receivedStdout(K3Process *, char *, int )),
117 SLOT(readOutput(K3Process *, char *, int)));
119 connect(&proc, SIGNAL( receivedStderr(K3Process *, char *, int )),
120 SLOT(readStdErr(K3Process *, char *, int)));
122 if (!proc.start( K3Process::Block, K3Process::AllOutput ))
124 error(KIO::ERR_CANNOT_LAUNCH_PROCESS,
125 "smbmount"+i18n("\nMake sure that the samba package is installed properly on your system."));
126 return;
129 kDebug(KIO_SMB) << "mount exit " << proc.exitStatus()
130 << "stdout:" << mybuf << endl << "stderr:" << mystderr << endl;
132 if (proc.exitStatus() != 0)
134 error( KIO::ERR_COULD_NOT_MOUNT,
135 i18n("Mounting of share \"%1\" from host \"%2\" by user \"%3\" failed.\n%4",
136 share, host, user, mybuf + '\n' + mystderr));
137 return;
140 finished();
142 break;
143 case 2:
144 case 4:
146 QString mountPoint;
147 stream >> mountPoint;
149 K3Process proc;
150 proc.setUseShell(true);
151 proc << "smbumount";
152 proc << KShell::quoteArg(mountPoint);
154 mybuf.truncate(0);
155 mystderr.truncate(0);
157 connect(&proc, SIGNAL( receivedStdout(K3Process *, char *, int )),
158 SLOT(readOutput(K3Process *, char *, int)));
160 connect(&proc, SIGNAL( receivedStderr(K3Process *, char *, int )),
161 SLOT(readStdErr(K3Process *, char *, int)));
163 if ( !proc.start( K3Process::Block, K3Process::AllOutput ) )
165 error(KIO::ERR_CANNOT_LAUNCH_PROCESS,
166 "smbumount"+i18n("\nMake sure that the samba package is installed properly on your system."));
167 return;
170 kDebug(KIO_SMB) << "smbumount exit " << proc.exitStatus()
171 << "stdout:" << mybuf << endl << "stderr:" << mystderr << endl;
173 if (proc.exitStatus() != 0)
175 error(KIO::ERR_COULD_NOT_UNMOUNT,
176 i18n("Unmounting of mountpoint \"%1\" failed.\n%2",
177 mountPoint, mybuf + '\n' + mystderr));
178 return;
181 if ( tmp == 4 )
183 bool ok;
185 QDir dir(mountPoint);
186 dir.cdUp();
187 ok = dir.rmdir(mountPoint);
188 if ( ok )
190 QString p=dir.path();
191 dir.cdUp();
192 ok = dir.rmdir(p);
195 if ( !ok )
197 error(KIO::ERR_COULD_NOT_RMDIR, mountPoint);
198 return;
202 finished();
204 break;
205 default:
206 break;
208 finished();
211 #include "kio_smb.moc"