KRename – powerful batch renamer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

permission.cpp 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /***************************************************************************
  2. permission.cpp - description
  3. -------------------
  4. begin : Sun Jan 13 2002
  5. copyright : (C) 2002 by Dominik Seichter
  6. email : domseichter@web.de
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include "permission.h"
  17. // QT includes
  18. #include <tqcheckbox.h>
  19. #include <tqgroupbox.h>
  20. #include <tqlabel.h>
  21. #include <tqlayout.h>
  22. // KDE includes
  23. #include <tdeapplication.h>
  24. #include <kcombobox.h>
  25. #include <kiconloader.h>
  26. #include <tdelocale.h>
  27. // OS includes
  28. #include <stdio.h>
  29. #include <pwd.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <grp.h>
  33. #include <unistd.h>
  34. const TQString MyPermPlugin::getName() const
  35. {
  36. return i18n("Permissions");
  37. }
  38. const TQString MyPermPlugin::getAccelName() const
  39. {
  40. return i18n("&Permissions");
  41. }
  42. const int MyPermPlugin::type() const
  43. {
  44. return TYPE_FINAL_FILE;
  45. }
  46. bool MyPermPlugin::checkError()
  47. {
  48. return true;
  49. }
  50. void MyPermPlugin::drawInterface( TQWidget* w, TQVBoxLayout* l )
  51. {
  52. int i;
  53. unsigned int uid; // Maybe this must be signed int ?
  54. struct passwd *user;
  55. struct group *ge;
  56. TQLabel *la, *cl[3];
  57. TQGridLayout *gl;
  58. TQString strOwner;
  59. m_widget = w;
  60. l->setResizeMode( TQLayout::FreeResize );
  61. w->setMinimumSize( TQSize( 100, 100 ) );
  62. Layout0 = new TQVBoxLayout( 0, 0, 6 );
  63. groupPermission = new TQGroupBox ( i18n("Access permissions"), w );
  64. groupPermission->setEnabled( FALSE );
  65. gl = new TQGridLayout (groupPermission, 6, 6, 15);
  66. gl->addRowSpacing(0, 10);
  67. checkPermissions = new TQCheckBox( i18n("Change &Permissions"), w );
  68. la = new TQLabel(i18n("Class"), groupPermission);
  69. gl->addWidget(la, 1, 0);
  70. la = new TQLabel( i18n("Read"), groupPermission );
  71. gl->addWidget (la, 1, 1);
  72. la = new TQLabel( i18n("Write"), groupPermission );
  73. gl->addWidget (la, 1, 2);
  74. la = new TQLabel( i18n("Exec"), groupPermission );
  75. TQSize size = la->sizeHint();
  76. size.setWidth(size.width() + 15);
  77. la->setFixedSize(size);
  78. gl->addWidget (la, 1, 3);
  79. la = new TQLabel( i18n("Special"), groupPermission );
  80. gl->addMultiCellWidget(la, 1, 1, 4, 5);
  81. cl[0] = new TQLabel( i18n("User"), groupPermission );
  82. gl->addWidget (cl[0], 2, 0);
  83. cl[1] = new TQLabel( i18n("Group"), groupPermission );
  84. gl->addWidget (cl[1], 3, 0);
  85. cl[2] = new TQLabel( i18n("Others"), groupPermission );
  86. gl->addWidget (cl[2], 4, 0);
  87. la = new TQLabel(i18n("UID"), groupPermission);
  88. gl->addWidget(la, 2, 5);
  89. la = new TQLabel(i18n("GID"), groupPermission);
  90. gl->addWidget(la, 3, 5);
  91. la = new TQLabel(i18n("Sticky"), groupPermission);
  92. gl->addWidget(la, 4, 5);
  93. for (int row = 0; row < 3 ; ++row) {
  94. for (int col = 0; col < 4; ++col) {
  95. TQCheckBox *cb = new TQCheckBox(groupPermission);
  96. permBox[row][col] = cb;
  97. gl->addWidget (permBox[row][col], row+2, col+1);
  98. permBox[row][0]->setChecked( TRUE );
  99. }
  100. }
  101. permBox[0][1]->setChecked( TRUE );
  102. gl->setColStretch(6, 10);
  103. checkOwner = new TQCheckBox( i18n("Change &Owner"), w );
  104. groupOwner = new TQGroupBox ( i18n("Ownership"), w );
  105. groupOwner->setEnabled( FALSE );
  106. groupOwner->setColumnLayout(0, Qt::Vertical );
  107. groupOwner->layout()->setSpacing( 6 );
  108. groupOwner->layout()->setMargin( 11 );
  109. groupOwnerLayout = new TQVBoxLayout( groupOwner->layout() );
  110. groupOwnerLayout->setAlignment( TQt::AlignTop );
  111. Layout2 = new TQHBoxLayout( 0, 0, 6 );
  112. Layout3 = new TQHBoxLayout( 0, 0, 6 );
  113. Layout4 = new TQHBoxLayout( 0, 0, 6 );
  114. la = new TQLabel( i18n("User:"), groupOwner );
  115. Layout2->addWidget( la );
  116. la = new TQLabel( i18n("Group:"), groupOwner );
  117. Layout3->addWidget( la );
  118. username = new KComboBox( groupOwner );
  119. uid = geteuid();
  120. setpwent();
  121. for (i=0; ((user = getpwent()) != 0L) && (i < MAXENTRIES); i++) {
  122. if( uid == 0 )
  123. username->insertItem(TQString::fromLatin1(user->pw_name));
  124. else
  125. if( user->pw_uid == uid )
  126. username->insertItem(TQString::fromLatin1(user->pw_name));
  127. }
  128. endpwent();
  129. groupname = new KComboBox( groupOwner );
  130. user = getpwuid(geteuid());
  131. TQString strUser = user->pw_name;
  132. setgrent();
  133. for (i=0; ((ge = getgrent()) != 0L) && (i < MAXENTRIES); i++) {
  134. if( uid == 0 ) {
  135. groupname->insertItem(TQString::fromLatin1(ge->gr_name));
  136. } else {
  137. char ** members = ge->gr_mem;
  138. char * member;
  139. while ((member = *members) != 0L) {
  140. if (strUser == member) {
  141. groupname->insertItem(TQString::fromLatin1(ge->gr_name));
  142. break;
  143. }
  144. ++members;
  145. }
  146. }
  147. }
  148. endgrent();
  149. /* add the users group */
  150. ge = getgrgid (getegid());
  151. if (ge) {
  152. TQString name = TQString::fromLatin1(ge->gr_name);
  153. if (name.isEmpty())
  154. name.setNum(ge->gr_gid);
  155. groupname->insertItem( name );
  156. }
  157. // make the users group visible
  158. groupname->setCurrentItem( groupname->count() );
  159. TQSpacerItem* spacer8 = new TQSpacerItem( 20, 20, TQSizePolicy::Minimum, TQSizePolicy::Expanding );
  160. TQSpacerItem* spacer9 = new TQSpacerItem( 20, 20, TQSizePolicy::Minimum, TQSizePolicy::Expanding );
  161. Layout2->addWidget( username );
  162. Layout2->addItem( spacer8 );
  163. Layout3->addWidget( groupname );
  164. Layout3->addItem( spacer9 );
  165. groupOwnerLayout->addLayout( Layout2 );
  166. groupOwnerLayout->addLayout( Layout3 );
  167. TQSpacerItem* spacer10 = new TQSpacerItem( 20, 20, TQSizePolicy::Minimum, TQSizePolicy::Expanding );
  168. Layout4->addWidget( checkPermissions );
  169. Layout4->addWidget( checkOwner );
  170. l->addLayout( Layout4 );
  171. l->addWidget( groupPermission );
  172. l->addWidget( groupOwner );
  173. l->addItem( spacer10 );
  174. connect( checkOwner, TQT_SIGNAL(clicked()), this, TQT_SLOT(enableControls()) );
  175. connect( checkPermissions, TQT_SIGNAL(clicked()), this, TQT_SLOT(enableControls()) );
  176. }
  177. void MyPermPlugin::fillStructure()
  178. {
  179. perm.changeOwner = checkOwner->isChecked();
  180. perm.changePermissions = checkPermissions->isChecked();
  181. if( perm.changeOwner ) {
  182. perm.group = groupname->currentText();
  183. perm.owner = username->currentText();
  184. }
  185. if( perm.changePermissions )
  186. perm.newPermission = getPermissions();
  187. }
  188. TQString MyPermPlugin::processFile( BatchRenamer* b, int i, TQString, int )
  189. {
  190. TQString filename = b->files()[i].dst.name;
  191. if( perm.changePermissions )
  192. if (chmod(filename.local8Bit(), (mode_t)perm.newPermission) == -1)
  193. return i18n("Can't chmod %1.").arg(filename);
  194. if( perm.changeOwner )
  195. if (chown(filename.local8Bit(), getUid(perm.owner), getGid(perm.group)))
  196. return i18n("Can't chown %1.").arg(filename);
  197. return TQString();
  198. }
  199. void MyPermPlugin::finished()
  200. {
  201. // We don't care about this event!
  202. return;
  203. }
  204. int MyPermPlugin::getPermissions()
  205. {
  206. int fperm[3][4] = {
  207. {S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID},
  208. {S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID},
  209. {S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX}
  210. };
  211. int permissions = 0, mask = 0;
  212. for (int row = 0;row < 3; ++row)
  213. for (int col = 0; col < 4; ++col)
  214. {
  215. switch (permBox[row][col]->state())
  216. {
  217. case TQCheckBox::On:
  218. permissions |= fperm[row][col];
  219. //fall through
  220. case TQCheckBox::Off:
  221. mask |= fperm[row][col];
  222. break;
  223. default:
  224. break;
  225. }
  226. }
  227. return permissions;
  228. }
  229. int MyPermPlugin::getGid( TQString group )
  230. {
  231. int i, r;
  232. struct group *ge;
  233. setgrent();
  234. for (i=0; ((ge = getgrent()) != 0L) && (i < MAXENTRIES); i++)
  235. if (!strcmp(ge->gr_name, group.local8Bit()))
  236. break;
  237. r = ge->gr_gid;
  238. endgrent();
  239. return r;
  240. }
  241. int MyPermPlugin::getUid( TQString owner )
  242. {
  243. int i, r;
  244. struct passwd *user;
  245. setpwent();
  246. for (i=0; ((user = getpwent()) != 0L) && (i < MAXENTRIES); i++)
  247. if (!strcmp(user->pw_name, owner.local8Bit()))
  248. break;
  249. r = user->pw_uid;
  250. endpwent();
  251. return r;
  252. }
  253. void MyPermPlugin::enableControls()
  254. {
  255. groupOwner->setEnabled( checkOwner->isChecked() );
  256. groupPermission->setEnabled( checkPermissions->isChecked() );
  257. }
  258. const TQPixmap MyPermPlugin::getIcon() const
  259. {
  260. return kapp->iconLoader()->loadIcon( "clanbomber", TDEIcon::Small );
  261. }
  262. #include "permission.moc"