"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/permissionsplugin.cpp" between
krename-4.0.9.tar.gz and krename-4.90.90.tar.gz

About: KRename is a batch file-renamer (KDE).

permissionsplugin.cpp  (krename-4.0.9):permissionsplugin.cpp  (krename-4.90.90)
skipping to change at line 18 skipping to change at line 18
/*************************************************************************** /***************************************************************************
* * * *
* This program is free software; you can redistribute it and/or modify * * This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * * it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or * * the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. * * (at your option) any later version. *
* * * *
***************************************************************************/ ***************************************************************************/
#ifndef _WIN32 #ifndef Q_OS_WIN
#include "permissionsplugin.h" #include "permissionsplugin.h"
#include <kiconloader.h> #include <kiconloader.h>
#include <klocale.h>
#include "ui_permissionspluginwidget.h" #include "ui_permissionspluginwidget.h"
#include <QDialog>
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QUrl>
// OS includes // OS includes
#include <stdio.h> #include <stdio.h>
#include <pwd.h> #include <pwd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <grp.h> #include <grp.h>
#include <unistd.h> #include <unistd.h>
// Only maxentries users are listed in the plugin // Only maxentries users are listed in the plugin
// increase if you need more // increase if you need more
#define MAXENTRIES 1000 #define MAXENTRIES 1000
PermissionsPlugin::PermissionsPlugin( PluginLoader* loader ) PermissionsPlugin::PermissionsPlugin(PluginLoader *loader)
: QObject( NULL ), Plugin( loader ), m_curPermission( S_IRUSR | S_IWUSR | S_ : QObject(nullptr), Plugin(loader), m_curPermission(S_IRUSR | S_IWUSR | S_IR
IRGRP ) GRP)
{ {
m_widget = new Ui::PermissionsPluginWidget(); m_widget = new Ui::PermissionsPluginWidget();
int i; int i;
uid_t uid = getuid(); uid_t uid = getuid();
// Get all users on the system // Get all users on the system
struct passwd* user; struct passwd *user;
setpwent(); setpwent();
for ( i=0; ((user = getpwent()) != 0L) && (i < MAXENTRIES); i++) for (i = 0; ((user = getpwent()) != 0L) && (i < MAXENTRIES); ++i) {
if( uid == 0 || uid == user->pw_uid ) if (uid == 0 || uid == user->pw_uid) {
{ m_users.append(QString::fromLatin1(user->pw_name));
m_users.append( QString::fromLatin1(user->pw_name) );
} }
}
endpwent(); endpwent();
// Get all groups on the system // Get all groups on the system
struct group *ge; struct group *ge;
user = getpwuid( uid ); user = getpwuid(uid);
setgrent(); setgrent();
for (i=0; ((ge = getgrent()) != 0L) && (i < MAXENTRIES); i++) for (i = 0; ((ge = getgrent()) != 0L) && (i < MAXENTRIES); ++i) {
{ if (uid == 0) {
if( uid == 0 )
{
// Add all groups if we are run as root // Add all groups if we are run as root
m_groups.append( QString::fromLatin1(ge->gr_name) ); m_groups.append(QString::fromLatin1(ge->gr_name));
} } else {
else
{
// If the current user is member of this group: add it // If the current user is member of this group: add it
char** members = ge->gr_mem; char **members = ge->gr_mem;
char* member; char *member;
while( (member = *members) != 0L ) while ((member = *members) != 0L) {
{ if (strcmp(user->pw_name, member) == 0) {
if( strcmp(user->pw_name,member) == 0 ) m_groups.append(QString::fromLatin1(ge->gr_name));
{
m_groups.append( QString::fromLatin1(ge->gr_name) );
break; break;
} }
++members; ++members;
} }
} }
} }
endgrent(); endgrent();
// add the users group // add the users group
ge = getgrgid ( user->pw_gid ); ge = getgrgid(user->pw_gid);
if( ge ) if (ge) {
{
QString name = QString::fromLatin1(ge->gr_name); QString name = QString::fromLatin1(ge->gr_name);
if (name.isEmpty()) if (name.isEmpty()) {
name.setNum(ge->gr_gid); name.setNum(ge->gr_gid);
}
m_groups.append( name ); m_groups.append(name);
} }
// sort both lists // sort both lists
m_users.sort(); m_users.sort();
m_groups.sort(); m_groups.sort();
} }
PermissionsPlugin::~PermissionsPlugin() PermissionsPlugin::~PermissionsPlugin()
{ {
delete m_widget; delete m_widget;
} }
const QString PermissionsPlugin::name() const const QString PermissionsPlugin::name() const
{ {
return i18n("Permissions"); return i18n("Permissions");
} }
const QPixmap PermissionsPlugin::icon() const const QPixmap PermissionsPlugin::icon() const
{ {
return KIconLoader::global()->loadIcon( "document-properties", KIconLoader:: NoGroup, KIconLoader::SizeSmall ); return KIconLoader::global()->loadIcon("document-properties", KIconLoader::N oGroup, KIconLoader::SizeSmall);
} }
QString PermissionsPlugin::processFile( BatchRenamer*, int, const QString & file nameOrToken, EPluginType ) QString PermissionsPlugin::processFile(BatchRenamer *, int, const QString &filen ameOrToken, EPluginType)
{ {
const QString & filename = filenameOrToken; const QString &filename = filenameOrToken;
if( !KUrl( filename ).isLocalFile() ) if (!QUrl(filename).isLocalFile()) {
return i18n("PermissionsPlugin works only with local files. %1 is a remo te file.", filename); return i18n("PermissionsPlugin works only with local files. %1 is a remo te file.", filename);
}
if( m_widget->checkPermissions->isChecked() ) if (m_widget->checkPermissions->isChecked()) {
{ if (chmod(filename.toUtf8().data(), (mode_t)m_curPermission) == -1) {
if( chmod( filename.toUtf8().data(), (mode_t)m_curPermission ) == -1 ) return i18n("Cannot chmod %1.", filename);
return i18n("Can't chmod %1.", filename); }
} }
if( m_widget->checkOwner->isChecked() ) if (m_widget->checkOwner->isChecked()) {
{ uid_t uid = getUid(m_widget->comboUser->currentText());
uid_t uid = getUid( m_widget->comboUser->currentText() ); gid_t gid = getGid(m_widget->comboGroup->currentText());
gid_t gid = getGid( m_widget->comboGroup->currentText() );
if( chown( filename.toUtf8().data(), uid, gid ) ) if (chown(filename.toUtf8().data(), uid, gid)) {
return i18n("Can't chown %1.", filename); return i18n("Cannot chown %1.", filename);
}
} }
return QString::null; return QString();
} }
void PermissionsPlugin::createUI( QWidget* parent ) const void PermissionsPlugin::createUI(QWidget *parent) const
{ {
m_widget->setupUi( parent ); m_widget->setupUi(parent);
m_widget->labelAdvanced->setVisible( false );
m_widget->comboUser->insertItems( 0, m_users );
m_widget->comboGroup->insertItems( 0, m_groups );
m_widget->comboPermOwner->setCurrentIndex( 2 ); m_widget->labelAdvanced->setVisible(false);
m_widget->comboPermGroup->setCurrentIndex( 1 );
m_widget->comboPermOthers->setCurrentIndex( 0 );
connect( m_widget->checkOwner, SIGNAL( clicked(bool) ), SLOT( slotEnableCont m_widget->comboUser->insertItems(0, m_users);
rols() ) ); m_widget->comboGroup->insertItems(0, m_groups);
connect( m_widget->checkPermissions, SIGNAL( clicked(bool) ), SLOT( slotEnab
leControls() ) ); m_widget->comboPermOwner->setCurrentIndex(2);
connect( m_widget->pushButton, SIGNAL( clicked(bool) ), SLOT( slotAdvancedPe m_widget->comboPermGroup->setCurrentIndex(1);
rmissions() ) ); m_widget->comboPermOthers->setCurrentIndex(0);
connect( m_widget->comboPermOwner, SIGNAL( activated(int) ), SLOT( slotUpdat
ePermissions() ) ); connect(m_widget->checkOwner, &QCheckBox::clicked,
connect( m_widget->comboPermGroup, SIGNAL( activated(int) ), SLOT( slotUpdat this, &PermissionsPlugin::slotEnableControls);
ePermissions() ) ); connect(m_widget->checkPermissions, &QCheckBox::clicked,
connect( m_widget->comboPermOthers, SIGNAL( activated(int) ), SLOT( slotUpda this, &PermissionsPlugin::slotEnableControls);
tePermissions() ) ); connect(m_widget->pushButton, &QPushButton::clicked,
connect( m_widget->checkFolder, SIGNAL( clicked(bool) ), SLOT( slotUpdatePer this, &PermissionsPlugin::slotAdvancedPermissions);
missions() ) ); connect(m_widget->comboPermOwner, static_cast<void (QComboBox::*)(int)>(&QCo
mboBox::activated),
this, &PermissionsPlugin::slotUpdatePermissions);
connect(m_widget->comboPermGroup, static_cast<void (QComboBox::*)(int)>(&QCo
mboBox::activated),
this, &PermissionsPlugin::slotUpdatePermissions);
connect(m_widget->comboPermOthers, static_cast<void (QComboBox::*)(int)>(&QC
omboBox::activated),
this, &PermissionsPlugin::slotUpdatePermissions);
connect(m_widget->checkFolder, &QCheckBox::clicked,
this, &PermissionsPlugin::slotUpdatePermissions);
} }
void PermissionsPlugin::slotEnableControls() void PermissionsPlugin::slotEnableControls()
{ {
m_widget->groupOwner->setEnabled( m_widget->checkOwner->isChecked() ); m_widget->groupOwner->setEnabled(m_widget->checkOwner->isChecked());
m_widget->groupPermissions->setEnabled( m_widget->checkPermissions->isChecke m_widget->groupPermissions->setEnabled(m_widget->checkPermissions->isChecked
d() ); ());
} }
void PermissionsPlugin::slotAdvancedPermissions() void PermissionsPlugin::slotAdvancedPermissions()
{ {
QDialog dialog; QDialog dialog;
QLabel *la, *cl[3]; QLabel *la, *cl[3];
QGridLayout* gl; QGridLayout *gl;
QCheckBox* permBox[3][4]; QCheckBox *permBox[3][4];
QVBoxLayout* layout = new QVBoxLayout( &dialog ); QVBoxLayout *layout = new QVBoxLayout(&dialog);
QGroupBox* groupPermission = new QGroupBox ( i18n("Access permissions"), &di QGroupBox *groupPermission = new QGroupBox(i18n("Access permissions"), &dial
alog ); og);
gl = new QGridLayout (groupPermission); gl = new QGridLayout(groupPermission);
//gl->addRowSpacing(0, 10); //gl->addRowSpacing(0, 10);
la = new QLabel(i18n("Class"), groupPermission); la = new QLabel(i18n("Class"), groupPermission);
gl->addWidget(la, 1, 0); gl->addWidget(la, 1, 0);
la = new QLabel( i18n("Read"), groupPermission ); la = new QLabel(i18n("Read"), groupPermission);
gl->addWidget (la, 1, 1); gl->addWidget(la, 1, 1);
la = new QLabel( i18n("Write"), groupPermission ); la = new QLabel(i18n("Write"), groupPermission);
gl->addWidget (la, 1, 2); gl->addWidget(la, 1, 2);
la = new QLabel( i18n("Exec"), groupPermission ); la = new QLabel(i18n("Exec"), groupPermission);
QSize size = la->sizeHint(); QSize size = la->sizeHint();
size.setWidth(size.width() + 15); size.setWidth(size.width() + 15);
la->setFixedSize(size); la->setFixedSize(size);
gl->addWidget (la, 1, 3); gl->addWidget(la, 1, 3);
la = new QLabel( i18n("Special"), groupPermission ); la = new QLabel(i18n("Special"), groupPermission);
gl->addWidget(la, 1, 4); gl->addWidget(la, 1, 4);
cl[0] = new QLabel( i18n("User"), groupPermission ); cl[0] = new QLabel(i18n("User"), groupPermission);
gl->addWidget (cl[0], 2, 0); gl->addWidget(cl[0], 2, 0);
cl[1] = new QLabel( i18n("Group"), groupPermission ); cl[1] = new QLabel(i18n("Group"), groupPermission);
gl->addWidget (cl[1], 3, 0); gl->addWidget(cl[1], 3, 0);
cl[2] = new QLabel( i18n("Others"), groupPermission ); cl[2] = new QLabel(i18n("Others"), groupPermission);
gl->addWidget (cl[2], 4, 0); gl->addWidget(cl[2], 4, 0);
la = new QLabel(i18n("UID"), groupPermission); la = new QLabel(i18n("UID"), groupPermission);
gl->addWidget(la, 2, 5); gl->addWidget(la, 2, 5);
la = new QLabel(i18n("GID"), groupPermission); la = new QLabel(i18n("GID"), groupPermission);
gl->addWidget(la, 3, 5); gl->addWidget(la, 3, 5);
la = new QLabel(i18n("Sticky"), groupPermission); la = new QLabel(i18n("Sticky"), groupPermission);
gl->addWidget(la, 4, 5); gl->addWidget(la, 4, 5);
int fperm[3][4] = { int fperm[3][4] = {
{S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID}, {S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID},
{S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID}, {S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID},
{S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX} {S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX}
}; };
for (int row = 0; row < 3 ; ++row) { for (int row = 0; row < 3 ; ++row) {
for (int col = 0; col < 4; ++col) { for (int col = 0; col < 4; ++col) {
QCheckBox *cb = new QCheckBox(groupPermission); QCheckBox *cb = new QCheckBox(groupPermission);
permBox[row][col] = cb; permBox[row][col] = cb;
gl->addWidget (permBox[row][col], row+2, col+1); gl->addWidget(permBox[row][col], row + 2, col + 1);
cb->setChecked( fperm[row][col] & m_curPermission ); cb->setChecked(fperm[row][col] & m_curPermission);
} }
} }
QDialogButtonBox* box = new QDialogButtonBox( QDialogButtonBox::Ok | QDialog QDialogButtonBox *box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogB
ButtonBox::Cancel, Qt::Horizontal, &dialog ); uttonBox::Cancel, Qt::Horizontal, &dialog);
connect( box, SIGNAL( accepted() ), &dialog, SLOT(accept() ) ); connect(box, &QDialogButtonBox::accepted,
connect( box, SIGNAL( rejected() ), &dialog, SLOT(reject() ) ); &dialog, &QDialog::accept);
connect(box, &QDialogButtonBox::rejected,
&dialog, &QDialog::reject);
layout->addWidget( groupPermission ); layout->addWidget(groupPermission);
layout->addWidget( box ); layout->addWidget(box);
if( dialog.exec() == QDialog::Accepted ) if (dialog.exec() == QDialog::Accepted) {
{
m_curPermission = 0; m_curPermission = 0;
for (int row = 0;row < 3; ++row) for (int row = 0; row < 3; ++row) {
{ for (int col = 0; col < 4; ++col) {
for (int col = 0; col < 4; ++col) if (permBox[row][col]->isChecked()) {
{
if( permBox[row][col]->isChecked() )
m_curPermission |= fperm[row][col]; m_curPermission |= fperm[row][col];
}
} }
} }
//setCurrentPermissions( m_curPermission ); //setCurrentPermissions( m_curPermission );
m_widget->labelAdvanced->setVisible( true ); m_widget->labelAdvanced->setVisible(true);
m_widget->comboPermOwner->setEnabled( false ); m_widget->comboPermOwner->setEnabled(false);
m_widget->comboPermGroup->setEnabled( false ); m_widget->comboPermGroup->setEnabled(false);
m_widget->comboPermOthers->setEnabled( false ); m_widget->comboPermOthers->setEnabled(false);
m_widget->checkFolder->setEnabled( false ); m_widget->checkFolder->setEnabled(false);
} }
} }
void PermissionsPlugin::slotUpdatePermissions() void PermissionsPlugin::slotUpdatePermissions()
{ {
int fpermUser [3] = { 0, S_IRUSR, S_IRUSR | S_IWUSR }; int fpermUser [3] = { 0, S_IRUSR, S_IRUSR | S_IWUSR };
int fpermGroup[3] = { 0, S_IRGRP, S_IRGRP | S_IWGRP }; int fpermGroup[3] = { 0, S_IRGRP, S_IRGRP | S_IWGRP };
int fpermOther[3] = { 0, S_IROTH, S_IROTH | S_IWOTH }; int fpermOther[3] = { 0, S_IROTH, S_IROTH | S_IWOTH };
m_curPermission = 0; m_curPermission = 0;
m_curPermission |= (fpermUser[m_widget->comboPermOwner->currentIndex()]); m_curPermission |= (fpermUser[m_widget->comboPermOwner->currentIndex()]);
m_curPermission |= (fpermGroup[m_widget->comboPermGroup->currentIndex()]); m_curPermission |= (fpermGroup[m_widget->comboPermGroup->currentIndex()]);
m_curPermission |= (fpermOther[m_widget->comboPermOthers->currentIndex()]); m_curPermission |= (fpermOther[m_widget->comboPermOthers->currentIndex()]);
m_widget->checkFolder->setTristate( false ); m_widget->checkFolder->setTristate(false);
if( m_widget->checkFolder->isChecked() ) if (m_widget->checkFolder->isChecked()) {
{ m_widget->checkFolder->setChecked(true);
m_widget->checkFolder->setChecked( true );
m_curPermission |= S_IXUSR; m_curPermission |= S_IXUSR;
m_curPermission |= S_IXGRP; m_curPermission |= S_IXGRP;
m_curPermission |= S_IXOTH; m_curPermission |= S_IXOTH;
} }
} }
int PermissionsPlugin::getGid( const QString & group ) const int PermissionsPlugin::getGid(const QString &group) const
{ {
int i, r = 0; int i, r = 0;
struct group *ge; struct group *ge;
setgrent(); setgrent();
for (i=0; ((ge = getgrent()) != 0L) && (i < MAXENTRIES); i++) for (i = 0; ((ge = getgrent()) != 0L) && (i < MAXENTRIES); i++)
if( !strcmp( ge->gr_name, group.toUtf8().data() ) ) if (!strcmp(ge->gr_name, group.toUtf8().data())) {
{
r = ge->gr_gid; r = ge->gr_gid;
break; break;
} }
endgrent(); endgrent();
return r; return r;
} }
int PermissionsPlugin::getUid( const QString & owner ) const int PermissionsPlugin::getUid(const QString &owner) const
{ {
int i, r = 0; int i, r = 0;
struct passwd *user; struct passwd *user;
setpwent(); setpwent();
for (i=0; ((user = getpwent()) != 0L) && (i < MAXENTRIES); i++) for (i = 0; ((user = getpwent()) != 0L) && (i < MAXENTRIES); i++)
if( !strcmp(user->pw_name, owner.toUtf8().data()) ) if (!strcmp(user->pw_name, owner.toUtf8().data())) {
{
r = user->pw_uid; r = user->pw_uid;
break; break;
} }
endpwent(); endpwent();
return r; return r;
} }
/* /*
void PermissionsPlugin::setCurrentPermissions( int perm ) void PermissionsPlugin::setCurrentPermissions( int perm )
skipping to change at line 377 skipping to change at line 377
{ {
if( (perm & S_IXUSR) || if( (perm & S_IXUSR) ||
(perm & S_IXGRP) || (perm & S_IXGRP) ||
(perm & S_IXOTH) ) (perm & S_IXOTH) )
m_widget->checkFolder->setCheckState( Qt::PartiallyChecked ); m_widget->checkFolder->setCheckState( Qt::PartiallyChecked );
} }
m_curPermission = perm; m_curPermission = perm;
} }
*/ */
#endif // Q_OS_WIN
#include "permissionsplugin.moc"
#endif // _WIN32
 End of changes. 56 change blocks. 
124 lines changed or deleted 120 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)