Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

filePermissions.cpp

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

Generated on Sat Nov 5 16:15:29 2005 for OPIE by  doxygen 1.4.2