• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kfile
kfilesharedialog.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 Copyright (c) 2001 David Faure <faure@kde.org>
3 Copyright (c) 2001 Laurent Montel <lmontel@mandrakesoft.com>
4
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 version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "kfilesharedialog.h"
21#include "kfsprocess.h"
22#include <kvbox.h>
23#include <QtGui/QLabel>
24#include <QtCore/QDir>
25#include <QtGui/QRadioButton>
26#include <QtGui/QButtonGroup>
27#include <QtGui/QLayout>
28#include <klocale.h>
29#include <kstandarddirs.h>
30#include <kdebug.h>
31#include <kio/kfileshare.h>
32#include <kseparator.h>
33#include <QtGui/QPushButton>
34#include <kmessagebox.h>
35
36class KFileSharePropsPlugin::Private
37{
38public:
39 KVBox *m_vBox;
40 KfsProcess *m_configProc;
41 bool m_bAllShared;
42 bool m_bAllUnshared;
43 QWidget *m_widget;
44 QRadioButton *m_rbShare;
45 QRadioButton *m_rbUnShare;
46 QPushButton *m_pbConfig;
47};
48
49KFileSharePropsPlugin::KFileSharePropsPlugin( KPropertiesDialog *_props )
50 : KPropertiesDialogPlugin( _props ),d(new Private)
51{
52 d->m_vBox = new KVBox();
53 _props->addPage( d->m_vBox, i18n("&Share") );
54
55 d->m_configProc = 0;
56 properties->setFileSharingPage(d->m_vBox);
57 d->m_widget = 0L;
58 init();
59}
60
61KFileSharePropsPlugin::~KFileSharePropsPlugin()
62{
63 if (d->m_configProc)
64 d->m_configProc->detach(); // Detach to prevent that we kill the process
65 delete d;
66}
67
68bool KFileSharePropsPlugin::supports( const KFileItemList& items )
69{
70 // Do not show dialog if in advanced mode,
71 // because the advanced dialog is shown already.
72 if (KFileShare::shareMode() == KFileShare::Advanced) {
73 kDebug() << "KFileSharePropsPlugin::supports: false because sharemode is advanced";
74 return false;
75 }
76
77 KFileItemList::const_iterator kit = items.begin();
78 const KFileItemList::const_iterator kend = items.end();
79 for ( ; kit != kend; ++kit )
80 {
81 bool isLocal = (*kit).isLocalFile();
82 // We only support local dirs
83 if ( !(*kit).isDir() || !isLocal )
84 return false;
85 }
86 return true;
87}
88
89void KFileSharePropsPlugin::init()
90{
91 // We store the main widget, so that it's possible (later) to call init()
92 // more than once, to update the page if something changed (e.g. after
93 // the user has been authorized)
94 delete d->m_widget;
95 d->m_rbShare = 0L;
96 d->m_rbUnShare = 0L;
97 d->m_widget = new QWidget( d->m_vBox );
98 QVBoxLayout * vbox = new QVBoxLayout( d->m_widget );
99
100 switch ( KFileShare::authorization() ) {
101 case KFileShare::Authorized:
102 {
103 // Check if all selected dirs are in $HOME
104 QString home = QDir::homePath();
105 if ( home[home.length()-1] != '/' )
106 home += '/';
107 bool ok = true;
108 const KFileItemList items = properties->items();
109 // We have 3 possibilities: all shared, all unshared, or mixed.
110 d->m_bAllShared = true;
111 d->m_bAllUnshared = true;
112 KFileItemList::const_iterator kit = items.begin();
113 const KFileItemList::const_iterator kend = items.end();
114 for ( ; kit != kend && ok; ++kit )
115 {
116 // We know it's local, see supports()
117 const QString path = (*kit).url().toLocalFile();
118 if ( !path.startsWith( home ) )
119 ok = false;
120 if ( KFileShare::isDirectoryShared( path ) )
121 d->m_bAllUnshared = false;
122 else
123 d->m_bAllShared = false;
124 }
125 if ( !ok )
126 {
127 vbox->addWidget( new QLabel( i18n( "Only folders in your home folder can be shared."),
128 d->m_widget ), 0 );
129 }
130 else
131 {
132 // Everything ok, show the share/unshare GUI
133 QButtonGroup *rbGroup = new QButtonGroup( d->m_widget );
134 d->m_rbUnShare = new QRadioButton( i18n("Not shared"), d->m_widget );
135 connect( d->m_rbUnShare, SIGNAL(toggled(bool)), SIGNAL(changed()) );
136 vbox->addWidget( d->m_rbUnShare, 0 );
137 rbGroup->addButton( d->m_rbUnShare );
138
139 d->m_rbShare = new QRadioButton( i18n("Shared"), d->m_widget );
140 connect( d->m_rbShare, SIGNAL(toggled(bool)), SIGNAL(changed()) );
141 vbox->addWidget( d->m_rbShare, 0 );
142 rbGroup->addButton( d->m_rbShare );
143
144 // Activate depending on status
145 if ( d->m_bAllShared )
146 d->m_rbShare->setChecked(true);
147 if ( d->m_bAllUnshared )
148 d->m_rbUnShare->setChecked(true);
149
150 // Some help text
151 QLabel *label = new QLabel( i18n("Sharing this folder makes it available under Linux/UNIX (NFS) and Windows (Samba).") , d->m_widget );
152 label->setAlignment( Qt::AlignLeft | Qt::AlignVCenter);
153 label->setWordWrap(true);
154 vbox->addWidget( label, 0 );
155
156 KSeparator* sep=new KSeparator(d->m_widget);
157 vbox->addWidget( sep, 0 );
158 label = new QLabel( i18n("You can also reconfigure file sharing authorization.") , d->m_widget );
159 label->setAlignment( Qt::AlignLeft | Qt::AlignVCenter);
160 label->setWordWrap(true);
161 vbox->addWidget( label, 0 );
162 d->m_pbConfig = new QPushButton( i18n("Configure File Sharing..."), d->m_widget );
163 connect( d->m_pbConfig, SIGNAL(clicked()), SLOT(slotConfigureFileSharing()) );
164 vbox->addWidget( d->m_pbConfig, 0, Qt::AlignHCenter );
165
166 vbox->addStretch( 10 );
167 }
168 }
169 break;
170 case KFileShare::ErrorNotFound:
171 vbox->addWidget( new QLabel( i18n("Error running 'filesharelist'. Check if installed and in $PATH or /usr/sbin."),
172 d->m_widget ), 0 );
173 break;
174 case KFileShare::UserNotAllowed:
175 {
176 vbox->setSpacing( 10 );
177 if (KFileShare::sharingEnabled()) {
178 vbox->addWidget( new QLabel( i18n("You need to be authorized to share folders."),
179 d->m_widget ), 0 );
180 } else {
181 vbox->addWidget( new QLabel( i18n("File sharing is disabled."),
182 d->m_widget ), 0 );
183 }
184 QHBoxLayout* hBox = new QHBoxLayout( (QWidget *)0L );
185 vbox->addLayout( hBox, 0 );
186 d->m_pbConfig = new QPushButton( i18n("Configure File Sharing..."), d->m_widget );
187 connect( d->m_pbConfig, SIGNAL(clicked()), SLOT(slotConfigureFileSharing()) );
188 hBox->addWidget( d->m_pbConfig, 0, Qt::AlignHCenter );
189 vbox->addStretch( 10 ); // align items on top
190 break;
191 }
192 case KFileShare::NotInitialized:
193 kWarning() << "KFileShare Authorization still NotInitialized after calling authorization() - impossible";
194 break;
195 }
196 d->m_widget->show(); // In case the dialog was shown already.
197}
198
199void KFileSharePropsPlugin::slotConfigureFileSharing()
200{
201 if (d->m_configProc) return;
202
203 d->m_configProc = new KfsProcess(this);
204 (*d->m_configProc) << KStandardDirs::findExe("kdesu") << "kcmshell4" << "fileshare";
205 if (!d->m_configProc->start())
206 {
207 delete d->m_configProc;
208 d->m_configProc = 0;
209 return;
210 }
211 connect(d->m_configProc, SIGNAL(processExited()),
212 this, SLOT(slotConfigureFileSharingDone()));
213 d->m_pbConfig->setEnabled(false);
214}
215
216void KFileSharePropsPlugin::slotConfigureFileSharingDone()
217{
218 delete d->m_configProc;
219 d->m_configProc = 0;
220 KFileShare::readConfig();
221 KFileShare::readShareList();
222 init();
223}
224
225void KFileSharePropsPlugin::applyChanges()
226{
227 kDebug() << "KFileSharePropsPlugin::applyChanges";
228 if ( d->m_rbShare && d->m_rbUnShare )
229 {
230 bool share = d->m_rbShare->isChecked();
231
232 if (share && d->m_bAllShared)
233 return; // Nothing to do
234 if (!share && d->m_bAllUnshared)
235 return; // Nothing to do
236
237 const KFileItemList items = properties->items();
238 bool ok = true;
239 KFileItemList::const_iterator kit = items.begin();
240 const KFileItemList::const_iterator kend = items.end();
241 for ( ; kit != kend && ok; ++kit )
242 {
243 const QString path = (*kit).url().toLocalFile();
244 ok = setShared( path, share );
245 if (!ok) {
246 if (share)
247 KMessageBox::detailedError(properties,
248 i18n("Sharing folder '%1' failed.", path),
249 i18n("An error occurred while trying to share folder '%1'. "
250 "Make sure that the Perl script 'fileshareset' is set suid root.",
251 path));
252 else
253 KMessageBox::error(properties,
254 i18n("Unsharing folder '%1' failed.", path),
255 i18n("An error occurred while trying to unshare folder '%1'. "
256 "Make sure that the Perl script 'fileshareset' is set suid root.",
257 path));
258
259 properties->abortApplying();
260 break;
261 }
262 }
263
264 // Get the change back into our cached info
265 KFileShare::readShareList();
266 }
267}
268
269bool KFileSharePropsPlugin::setShared( const QString& path, bool shared )
270{
271 kDebug() << "KFileSharePropsPlugin::setShared " << path << "," << shared;
272 return KFileShare::setShared( path, shared );
273}
274
275QWidget* KFileSharePropsPlugin::page() const
276{
277 return d->m_vBox;
278}
279
280#include "kfilesharedialog.moc"
281
282//TODO: do we need to monitor /etc/security/fileshare.conf ?
283// if the user is added to the 'fileshare' group, we wouldn't be notified
284// Of course the config module can notify us.
285// TODO: listen to such notifications ;)
KFileItemList
List of KFileItems, which adds a few helper methods to QList<KFileItem>.
Definition: kfileitem.h:675
KFileSharePropsPlugin::slotConfigureFileSharingDone
void slotConfigureFileSharingDone()
Definition: kfilesharedialog.cpp:216
KFileSharePropsPlugin::KFileSharePropsPlugin
KFileSharePropsPlugin(KPropertiesDialog *_props)
Definition: kfilesharedialog.cpp:49
KFileSharePropsPlugin::~KFileSharePropsPlugin
virtual ~KFileSharePropsPlugin()
Definition: kfilesharedialog.cpp:61
KFileSharePropsPlugin::slotConfigureFileSharing
void slotConfigureFileSharing()
Definition: kfilesharedialog.cpp:199
KFileSharePropsPlugin::page
QWidget * page() const
Definition: kfilesharedialog.cpp:275
KFileSharePropsPlugin::supports
static bool supports(const KFileItemList &items)
Definition: kfilesharedialog.cpp:68
KFileSharePropsPlugin::applyChanges
virtual void applyChanges()
Apply all changes to the file.
Definition: kfilesharedialog.cpp:225
KMessageBox::error
static void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
KMessageBox::detailedError
static void detailedError(QWidget *parent, const QString &text, const QString &details, const QString &caption=QString(), Options options=Notify)
KPageDialog::addPage
void addPage(KPageWidgetItem *item)
KPropertiesDialogPlugin
A Plugin in the Properties dialog This is an abstract class.
Definition: kpropertiesdialog.h:348
KPropertiesDialogPlugin::properties
KPropertiesDialog * properties
Pointer to the dialog.
Definition: kpropertiesdialog.h:393
KPropertiesDialogPlugin::changed
void changed()
Emit this signal when the user changed anything in the plugin's tabs.
KPropertiesDialog
The main properties dialog class.
Definition: kpropertiesdialog.h:58
KPropertiesDialog::items
KFileItemList items() const
Definition: kpropertiesdialog.cpp:404
KPropertiesDialog::setFileSharingPage
void setFileSharingPage(QWidget *page)
Sets the file sharing page.
Definition: kpropertiesdialog.cpp:361
KPropertiesDialog::abortApplying
void abortApplying()
To abort applying changes.
Definition: kpropertiesdialog.cpp:594
KSeparator
KStandardDirs::findExe
static QString findExe(const QString &appname, const QString &pathstr=QString(), SearchOptions options=NoSearchOptions)
KVBox
KfsProcess
Definition: kfsprocess.h:67
QLabel
QPushButton
QWidget
kDebug
#define kDebug
kWarning
#define kWarning
kdebug.h
kfileshare.h
kfilesharedialog.h
kfsprocess.h
klocale.h
i18n
QString i18n(const char *text)
kmessagebox.h
kseparator.h
kstandarddirs.h
share
QString share()
kvbox.h
KFileShare::shareMode
ShareMode shareMode()
Returns the configured share mode.
Definition: kfileshare.cpp:139
KFileShare::readShareList
void readShareList()
Reads the list of shared folders.
Definition: kfileshare.cpp:183
KFileShare::isDirectoryShared
bool isDirectoryShared(const QString &path)
Call this to know if a directory is currently shared.
Definition: kfileshare.cpp:216
KFileShare::sharingEnabled
bool sharingEnabled()
Returns whether sharing is enabled If this is false, file sharing is disabled and nobody can share fi...
Definition: kfileshare.cpp:146
KFileShare::authorization
Authorization authorization()
Call this to know if the current user is authorized to share directories.
Definition: kfileshare.cpp:227
KFileShare::UserNotAllowed
@ UserNotAllowed
Definition: kfileshare.h:48
KFileShare::NotInitialized
@ NotInitialized
Definition: kfileshare.h:48
KFileShare::Authorized
@ Authorized
Definition: kfileshare.h:48
KFileShare::ErrorNotFound
@ ErrorNotFound
Definition: kfileshare.h:48
KFileShare::setShared
bool setShared(const QString &path, bool shared)
Uses a suid perl script to share the given path with NFS and Samba.
Definition: kfileshare.cpp:235
KFileShare::readConfig
void readConfig()
Reads the file share configuration file.
Definition: kfileshare.cpp:106
KFileShare::Advanced
@ Advanced
Definition: kfileshare.h:70
home
KAction * home(const QObject *recvr, const char *slot, QObject *parent)
ok
KGuiItem ok()
label
QString label(StandardShortcut id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal