00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "filePermissions.h"
00015
00016 #include <qfileinfo.h>
00017
00018 #include <qcheckbox.h>
00019 #include <qlabel.h>
00020 #include <qlineedit.h>
00021 #include <qmessagebox.h>
00022
00023 #include <unistd.h>
00024 #include <sys/stat.h>
00025 #include <stdlib.h>
00026 #include <sys/types.h>
00027 #include <pwd.h>
00028 #include <grp.h>
00029
00030 filePermissions::filePermissions( QWidget* parent, const char* name, bool modal, WFlags fl, const QString &fileName, bool useRemote )
00031 : QDialog( parent, name, modal, fl )
00032 {
00033 if ( !name )
00034 setName( tr("filePermissions") );
00035
00036 resize( 236, 210 );
00037 isRemote=useRemote;
00038 setMaximumSize( QSize( 236, 210 ) );
00039 setCaption( tr( "Set File Permissions" ) );
00040
00041 TextLabel1 = new QLabel( this, "TextLabel1" );
00042 TextLabel1->setGeometry( QRect( 25, 5, 175, 20 ) );
00043 TextLabel1->setText( tr( "Set file permissions for:" ) );
00044
00045 LineEdit1 = new QLineEdit( this, "LineEdit1" );
00046 LineEdit1->setGeometry( QRect( 10, 25, 218, 22 ) );
00047 LineEdit1->setReadOnly(true);
00048
00049 TextLabel4 = new QLabel( this, "TextLabel4" );
00050 TextLabel4->setGeometry( QRect( 5, 85, 50, 15 ) );
00051 TextLabel4->setText( tr( "owner" ) );
00052
00053 TextLabel4_2 = new QLabel( this, "TextLabel4_2" );
00054 TextLabel4_2->setGeometry( QRect( 5, 105, 50, 15 ) );
00055 TextLabel4_2->setText( tr( "group" ) );
00056
00057 TextLabel4_3 = new QLabel( this, "TextLabel4_3" );
00058 TextLabel4_3->setGeometry( QRect( 5, 125, 50, 15 ) );
00059 TextLabel4_3->setText( tr( "others" ) );
00060
00061 CheckBox1 = new QCheckBox( this, "CheckBox1" );
00062 CheckBox1->setGeometry( QRect( 75, 85, 20, 16 ) );
00063 connect(CheckBox1, SIGNAL(released()),this,SLOT(ownReadCheck()));
00064
00065 CheckBox1_2 = new QCheckBox( this, "CheckBox1_2" );
00066 CheckBox1_2->setGeometry( QRect( 135, 85, 20, 16 ) );
00067 connect(CheckBox1_2, SIGNAL(released()),this,SLOT(ownWriteCheck()));
00068
00069 CheckBox1_3 = new QCheckBox( this, "CheckBox1_3" );
00070 CheckBox1_3->setGeometry( QRect( 195, 85, 20, 16 ) );
00071 connect(CheckBox1_3, SIGNAL(released()),this,SLOT(ownExeCheck()));
00072
00073 CheckBox1_4 = new QCheckBox( this, "CheckBox1_4" );
00074 CheckBox1_4->setGeometry( QRect( 75, 105, 20, 16 ) );
00075 connect(CheckBox1_4, SIGNAL(released()),this,SLOT(grpReadCheck()));
00076
00077 CheckBox1_5 = new QCheckBox( this, "CheckBox1_5" );
00078 CheckBox1_5->setGeometry( QRect( 135, 105, 20, 16 ) );
00079 connect(CheckBox1_5, SIGNAL(released()),this,SLOT(grpWriteCheck()));
00080
00081 CheckBox1_6 = new QCheckBox( this, "CheckBox1_6" );
00082 CheckBox1_6->setGeometry( QRect( 195, 105, 20, 16 ) );
00083 connect(CheckBox1_6, SIGNAL(released()),this,SLOT(grpExeCheck()));
00084
00085 CheckBox1_7 = new QCheckBox( this, "CheckBox1_7" );
00086 CheckBox1_7->setGeometry( QRect( 75, 125, 16, 16 ) );
00087 connect(CheckBox1_7, SIGNAL(released()),this,SLOT(wrldReadCheck()));
00088
00089 CheckBox1_8 = new QCheckBox( this, "CheckBox1_8" );
00090 CheckBox1_8->setGeometry( QRect( 135, 125, 20, 16 ) );
00091 connect(CheckBox1_8, SIGNAL(released()),this,SLOT(wrldWriteCheck()));
00092
00093 CheckBox1_8_2 = new QCheckBox( this, "CheckBox1_8_2" );
00094 CheckBox1_8_2->setGeometry( QRect( 195, 125, 20, 16 ) );
00095 connect(CheckBox1_8_2, SIGNAL(released()),this,SLOT(wrldExeCheck()));
00096
00097 GroupLineEdit = new QLineEdit( this, "GroupLineEdit" );
00098 GroupLineEdit->setGeometry( QRect( 125, 155, 106, 22 ) );
00099
00100 OwnerLineEdit = new QLineEdit( this, "OwnerLineEdit" );
00101 OwnerLineEdit->setGeometry( QRect( 10, 155, 106, 22 ) );
00102
00103 TextLabel5 = new QLabel( this, "TextLabel5" );
00104 TextLabel5->setGeometry( QRect( 45, 180, 40, 16 ) );
00105 TextLabel5->setText( tr( "Owner" ) );
00106
00107 TextLabel5_2 = new QLabel( this, "TextLabel5_2" );
00108 TextLabel5_2->setGeometry( QRect( 155, 180, 40, 16 ) );
00109 TextLabel5_2->setText( tr( "Group" ) );
00110
00111 ModeLine = new QLineEdit( this, "TextLabelMode" );
00112 ModeLine->setGeometry( QRect( 10, 60, 40, 15 ) );
00113
00114 TextLabel3_2 = new QLabel( this, "TextLabel3_2" );
00115 TextLabel3_2->setGeometry( QRect( 60, 55, 50, 20 ) );
00116 TextLabel3_2->setText( tr( "read" ) );
00117 TextLabel3_2->setAlignment( int( QLabel::AlignBottom | QLabel::AlignHCenter ) );
00118
00119 TextLabel3_2_2 = new QLabel( this, "TextLabel3_2_2" );
00120 TextLabel3_2_2->setGeometry( QRect( 120, 55, 50, 20 ) );
00121 TextLabel3_2_2->setText( tr( "write" ) );
00122 TextLabel3_2_2->setAlignment( int( QLabel::AlignBottom | QLabel::AlignHCenter ) );
00123
00124 TextLabel3 = new QLabel( this, "TextLabel3" );
00125 TextLabel3->setGeometry( QRect( 180, 55, 50, 20 ) );
00126 TextLabel3->setText( tr( "execute" ) );
00127 TextLabel3->setAlignment( int( QLabel::AlignBottom | QLabel::AlignHCenter ) );
00128
00129 struct stat buf;
00130 mode_t mode;
00131 file = fileName;
00132 QFileInfo fi(file);
00133
00134 LineEdit1->setText( file);
00135 OwnerLineEdit->setText( fi.owner());
00136 GroupLineEdit->setText( fi.group());
00137
00138 if( fi.permission( QFileInfo::ReadUser)) { CheckBox1->setChecked(true); }
00139 if( fi.permission( QFileInfo::WriteUser)) { CheckBox1_2->setChecked(true); }
00140 if( fi.permission( QFileInfo::ExeUser)) { CheckBox1_3->setChecked(true); }
00141
00142 if( fi.permission( QFileInfo::ReadGroup)) { CheckBox1_4->setChecked(true); }
00143 if( fi.permission( QFileInfo::WriteGroup)) { CheckBox1_5->setChecked(true); }
00144 if( fi.permission( QFileInfo::ExeGroup)) { CheckBox1_6->setChecked(true); }
00145
00146 if( fi.permission( QFileInfo::ReadOther)) { CheckBox1_7->setChecked(true); }
00147 if( fi.permission( QFileInfo::WriteOther)) { CheckBox1_8->setChecked(true); }
00148 if( fi.permission( QFileInfo::ExeOther)) { CheckBox1_8_2->setChecked(true); }
00149
00150 stat(file.latin1(), &buf);
00151 mode = buf.st_mode;
00152 modeStr.sprintf("%#o", buf.st_mode & ~(S_IFMT) );
00153 ModeLine->setText(modeStr);
00154 bool ok;
00155 i_mode = modeStr.toInt(&ok,10);
00156
00157 }
00158
00159
00160
00161
00162 filePermissions::~filePermissions()
00163 {
00164 }
00165
00166
00167
00168 void filePermissions::ownReadCheck() {
00169 if(CheckBox1->isChecked()) { i_mode +=400; }
00170 else i_mode -=400;
00171 modeStr.sprintf("0%d",i_mode);
00172 ModeLine->setText( modeStr);
00173
00174 }
00175
00176 void filePermissions::ownWriteCheck() {
00177 if(CheckBox1_2->isChecked()) { i_mode +=200; }
00178 else i_mode -=200;
00179 modeStr.sprintf("0%d",i_mode);
00180 ModeLine->setText(modeStr);
00181
00182 }
00183
00184 void filePermissions::ownExeCheck() {
00185 if(CheckBox1_3->isChecked()) { i_mode +=100; }
00186 else i_mode -=100;
00187 modeStr.sprintf("0%d",i_mode);
00188 ModeLine->setText(modeStr);
00189
00190 }
00191
00192 void filePermissions::grpReadCheck() {
00193 if(CheckBox1_4->isChecked()) { i_mode +=40; }
00194 else i_mode -=40;
00195 modeStr.sprintf("0%d",i_mode);
00196 ModeLine->setText(modeStr);
00197
00198 }
00199
00200 void filePermissions::grpWriteCheck() {
00201 if(CheckBox1_5->isChecked()) { i_mode +=20; }
00202 else i_mode -=20;
00203 modeStr.sprintf("0%d",i_mode);
00204 ModeLine->setText(modeStr);
00205
00206 }
00207
00208 void filePermissions::grpExeCheck() {
00209 if(CheckBox1_6->isChecked()) { i_mode +=10; }
00210 else i_mode -=10;
00211 modeStr.sprintf("0%d",i_mode);
00212 ModeLine->setText(modeStr);
00213
00214 }
00215
00216 void filePermissions::wrldReadCheck() {
00217 if(CheckBox1_7->isChecked()) { i_mode +=4; }
00218 else i_mode -=4;
00219 modeStr.sprintf("0%d",i_mode);
00220 ModeLine->setText(modeStr);
00221
00222 }
00223
00224 void filePermissions::wrldWriteCheck() {
00225 if(CheckBox1_8->isChecked()) { i_mode +=2; }
00226 else i_mode -=2;
00227 modeStr.sprintf("0%d",i_mode);
00228 ModeLine->setText(modeStr);
00229
00230 }
00231
00232 void filePermissions::wrldExeCheck() {
00233 if(CheckBox1_8_2->isChecked()) { i_mode +=1; }
00234 else i_mode -=1;
00235 modeStr.sprintf("0%d",i_mode);
00236 ModeLine->setText(modeStr);
00237
00238 }
00239
00240 void filePermissions::accept() {
00241 QFileInfo fi(file);
00242 struct passwd *pwd=0;
00243 struct group *grp=0;
00244 pwd = getpwnam(OwnerLineEdit->text().latin1() );
00245 if(pwd == NULL) {
00246 perror("getpwnam");
00247 QMessageBox::warning(this,tr("Warning"),tr("Error- no user"));
00248 return;
00249 } else {
00250 grp = getgrnam(GroupLineEdit->text().latin1());
00251 if(grp==NULL) {
00252 perror("getgrnam");
00253 QMessageBox::warning(this,tr("Warning"),tr("Error- no group"));
00254 return;
00255 }
00256 if( chown( file.latin1(), pwd->pw_uid, grp->gr_gid) <0) {
00257 perror("chown");
00258 QMessageBox::warning(this,tr("Warning"),tr("Error setting ownership or group"));
00259 return;
00260 }
00261 bool ok;
00262 moder = modeStr.toUInt(&ok,8);
00263 if (!isRemote) {
00264 if( chmod( file.latin1(), moder) < 0) {
00265 perror("chmod");
00266 QMessageBox::warning(this,tr("Warning"),tr("Error setting mode"));
00267 return;
00268 }
00269 }
00270 }
00271 close();
00272 }