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 <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 )
00031     : QDialog( parent, name, modal, fl )
00032 {
00033     if ( !name )
00034   setName( tr("filePermissions") );
00035 //    odebug << "FilePermissions "+fileName << oendl; 
00036 /*    resize( 236, 210 );
00037     setMaximumSize( QSize( 236, 210 ) );
00038 */    setCaption( tr( "Set File Permissions" ) );
00039 
00040     TextLabel1 = new QLabel( this, "TextLabel1" );
00041     TextLabel1->setGeometry( QRect( 25, 5, 175, 20 ) );
00042     TextLabel1->setText( tr( "Set file permissions for:" ) );
00043 
00044     LineEdit1 = new QLineEdit( this, "LineEdit1" );
00045     LineEdit1->setGeometry( QRect( 10, 25, 218, 22 ) );
00046     LineEdit1->setReadOnly(true);
00047 
00048     TextLabel4 = new QLabel( this, "TextLabel4" );
00049     TextLabel4->setGeometry( QRect( 5, 85, 50, 15 ) );
00050     TextLabel4->setText( tr( "owner" ) );
00051 
00052     TextLabel4_2 = new QLabel( this, "TextLabel4_2" );
00053     TextLabel4_2->setGeometry( QRect( 5, 105, 50, 15 ) );
00054     TextLabel4_2->setText( tr( "group" ) );
00055 
00056     TextLabel4_3 = new QLabel( this, "TextLabel4_3" );
00057     TextLabel4_3->setGeometry( QRect( 5, 125, 50, 15 ) );
00058     TextLabel4_3->setText( tr( "others" ) );
00059 
00060     CheckBox1 = new QCheckBox( this, "CheckBox1" );
00061     CheckBox1->setGeometry( QRect( 75, 85, 20, 16 ) );
00062     connect(CheckBox1, SIGNAL(released()),this,SLOT(ownReadCheck()));
00063 
00064     CheckBox1_2 = new QCheckBox( this, "CheckBox1_2" );
00065     CheckBox1_2->setGeometry( QRect( 135, 85, 20, 16 ) );
00066     connect(CheckBox1_2, SIGNAL(released()),this,SLOT(ownWriteCheck()));
00067 
00068     CheckBox1_3 = new QCheckBox( this, "CheckBox1_3" );
00069     CheckBox1_3->setGeometry( QRect( 195, 85, 20, 16 ) );
00070     connect(CheckBox1_3, SIGNAL(released()),this,SLOT(ownExeCheck()));
00071 
00072     CheckBox1_4 = new QCheckBox( this, "CheckBox1_4" );
00073     CheckBox1_4->setGeometry( QRect( 75, 105, 20, 16 ) );
00074     connect(CheckBox1_4, SIGNAL(released()),this,SLOT(grpReadCheck()));
00075 
00076     CheckBox1_5 = new QCheckBox( this, "CheckBox1_5" );
00077     CheckBox1_5->setGeometry( QRect( 135, 105, 20, 16 ) );
00078     connect(CheckBox1_5, SIGNAL(released()),this,SLOT(grpWriteCheck()));
00079 
00080     CheckBox1_6 = new QCheckBox( this, "CheckBox1_6" );
00081     CheckBox1_6->setGeometry( QRect( 195, 105, 20, 16 ) );
00082     connect(CheckBox1_6, SIGNAL(released()),this,SLOT(grpExeCheck()));
00083 
00084     CheckBox1_7 = new QCheckBox( this, "CheckBox1_7" );
00085     CheckBox1_7->setGeometry( QRect( 75, 125, 16, 16 ) );
00086     connect(CheckBox1_7, SIGNAL(released()),this,SLOT(wrldReadCheck()));
00087 
00088     CheckBox1_8 = new QCheckBox( this, "CheckBox1_8" );
00089     CheckBox1_8->setGeometry( QRect( 135, 125, 20, 16 ) );
00090     connect(CheckBox1_8, SIGNAL(released()),this,SLOT(wrldWriteCheck()));
00091 
00092     CheckBox1_8_2 = new QCheckBox( this, "CheckBox1_8_2" );
00093     CheckBox1_8_2->setGeometry( QRect( 195, 125, 20, 16 ) );
00094     connect(CheckBox1_8_2, SIGNAL(released()),this,SLOT(wrldExeCheck()));
00095 
00096     GroupLineEdit = new QLineEdit( this, "GroupLineEdit" );
00097     GroupLineEdit->setGeometry( QRect( 125, 155, 106, 22 ) );
00098 
00099     OwnerLineEdit = new QLineEdit( this, "OwnerLineEdit" );
00100     OwnerLineEdit->setGeometry( QRect( 10, 155, 106, 22 ) );
00101 
00102     TextLabel5 = new QLabel( this, "TextLabel5" );
00103     TextLabel5->setGeometry( QRect( 45, 180, 40, 16 ) );
00104     TextLabel5->setText( tr( "Owner" ) );
00105 
00106     TextLabel5_2 = new QLabel( this, "TextLabel5_2" );
00107     TextLabel5_2->setGeometry( QRect( 155, 180, 40, 16 ) );
00108     TextLabel5_2->setText( tr( "Group" ) );
00109 
00110     ModeLine = new QLineEdit( this, "TextLabelMode" );
00111     ModeLine->setGeometry( QRect( 10, 60, 40, 15 ) );
00112 
00113     TextLabel3_2 = new QLabel( this, "TextLabel3_2" );
00114     TextLabel3_2->setGeometry( QRect( 60, 55, 50, 20 ) );
00115     TextLabel3_2->setText( tr( "read" ) );
00116     TextLabel3_2->setAlignment( int( QLabel::AlignBottom | QLabel::AlignHCenter ) );
00117 
00118     TextLabel3_2_2 = new QLabel( this, "TextLabel3_2_2" );
00119     TextLabel3_2_2->setGeometry( QRect( 120, 55, 50, 20 ) );
00120     TextLabel3_2_2->setText( tr( "write" ) );
00121     TextLabel3_2_2->setAlignment( int( QLabel::AlignBottom | QLabel::AlignHCenter ) );
00122 
00123     TextLabel3 = new QLabel( this, "TextLabel3" );
00124     TextLabel3->setGeometry( QRect( 180, 55, 50, 20 ) );
00125     TextLabel3->setText( tr( "execute" ) );
00126     TextLabel3->setAlignment( int( QLabel::AlignBottom | QLabel::AlignHCenter ) );
00127 
00128     struct stat buf;
00129     mode_t mode;
00130     file = fileName;
00131     QFileInfo fi(file);
00132 
00133     LineEdit1->setText( file);
00134     OwnerLineEdit->setText( fi.owner());
00135     GroupLineEdit->setText( fi.group());
00136 
00137     if( fi.permission( QFileInfo::ReadUser)) { CheckBox1->setChecked(true); }
00138     if( fi.permission( QFileInfo::WriteUser)) { CheckBox1_2->setChecked(true); }
00139     if( fi.permission( QFileInfo::ExeUser)) { CheckBox1_3->setChecked(true); }
00140 
00141     if( fi.permission( QFileInfo::ReadGroup)) { CheckBox1_4->setChecked(true); }
00142     if( fi.permission( QFileInfo::WriteGroup)) { CheckBox1_5->setChecked(true); }
00143     if( fi.permission( QFileInfo::ExeGroup)) { CheckBox1_6->setChecked(true); }
00144 
00145     if( fi.permission( QFileInfo::ReadOther)) { CheckBox1_7->setChecked(true); }
00146     if( fi.permission( QFileInfo::WriteOther)) { CheckBox1_8->setChecked(true); }
00147     if( fi.permission( QFileInfo::ExeOther)) { CheckBox1_8_2->setChecked(true); }
00148 
00149     stat(file.latin1(), &buf);
00150     mode = buf.st_mode;
00151     modeStr.sprintf("%#o", buf.st_mode & ~(S_IFMT) );
00152     ModeLine->setText(modeStr);
00153     bool ok;
00154     i_mode = modeStr.toInt(&ok,10);
00155 
00156 }
00157 
00158 /*
00159  *  Destroys the object and frees any allocated resources
00160  */
00161 filePermissions::~filePermissions()
00162 {
00163 }
00164 
00165 
00166 // might this be better as a callback routine???
00167 void filePermissions::ownReadCheck() {
00168     if(CheckBox1->isChecked()) { i_mode +=400; }
00169     else i_mode -=400;
00170     modeStr.sprintf("0%d",i_mode);
00171     ModeLine->setText( modeStr);
00172 // 0400
00173 }
00174 
00175 void filePermissions::ownWriteCheck() {
00176     if(CheckBox1_2->isChecked()) { i_mode +=200; }
00177     else i_mode -=200;
00178     modeStr.sprintf("0%d",i_mode);
00179     ModeLine->setText(modeStr);
00180 // 0200
00181 }
00182 
00183 void filePermissions::ownExeCheck() {
00184     if(CheckBox1_3->isChecked()) { i_mode +=100; }
00185     else i_mode -=100;
00186     modeStr.sprintf("0%d",i_mode);
00187     ModeLine->setText(modeStr);
00188 // 0100
00189 }
00190 
00191 void filePermissions::grpReadCheck() {
00192     if(CheckBox1_4->isChecked()) { i_mode +=40; }
00193     else i_mode -=40;
00194     modeStr.sprintf("0%d",i_mode);
00195     ModeLine->setText(modeStr);
00196 // 0040
00197 }
00198 
00199 void filePermissions::grpWriteCheck() {
00200     if(CheckBox1_5->isChecked()) { i_mode +=20; }
00201     else i_mode -=20;
00202     modeStr.sprintf("0%d",i_mode);
00203     ModeLine->setText(modeStr);
00204 // 0020
00205 }
00206 
00207 void filePermissions::grpExeCheck() {
00208     if(CheckBox1_6->isChecked()) { i_mode +=10; }
00209     else i_mode -=10;
00210     modeStr.sprintf("0%d",i_mode);
00211     ModeLine->setText(modeStr);
00212 // 0010
00213 }
00214 
00215 void filePermissions::wrldReadCheck() {
00216     if(CheckBox1_7->isChecked()) { i_mode +=4; }
00217     else i_mode -=4;
00218     modeStr.sprintf("0%d",i_mode);
00219     ModeLine->setText(modeStr);
00220 // 0004
00221 }
00222 
00223 void filePermissions::wrldWriteCheck() {
00224     if(CheckBox1_8->isChecked()) { i_mode +=2; }
00225     else i_mode -=2;
00226     modeStr.sprintf("0%d",i_mode);
00227     ModeLine->setText(modeStr);
00228 // 0002
00229 }
00230 
00231 void filePermissions::wrldExeCheck() {
00232     if(CheckBox1_8_2->isChecked()) { i_mode +=1; }
00233     else i_mode -=1;
00234     modeStr.sprintf("0%d",i_mode);
00235     ModeLine->setText(modeStr);
00236 // 0001
00237 }
00238 
00239 void filePermissions::accept() {
00240 
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         uint moder = modeStr.toUInt(&ok,8);
00263         if( chmod( file.latin1(), moder) < 0) {
00264             perror("chmod");
00265             QMessageBox::warning(this,tr("Warning"),tr("Error setting mode") );
00266             return;
00267         }
00268     }
00269     close();
00270 }

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