kauthicon.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (c) 1999 Preston Brown <pbrown@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 /* 00019 * KAuthIcon - an icon which shows whether privileges are in effect 00020 */ 00021 00022 #include <unistd.h> // For getuid 00023 00024 #include <tqlayout.h> 00025 #include <tqlabel.h> 00026 #include <tqtimer.h> 00027 00028 #include <klocale.h> 00029 00030 #include "kauthicon.h" 00031 00032 /* XPM */ 00033 static const char * const lock_xpm[] = { 00034 "22 22 5 1", 00035 " c None", 00036 ". c #808080", 00037 "+ c #000000", 00038 "@ c #FFFFFF", 00039 "# c #C0C0C0", 00040 " ", 00041 " ", 00042 " ", 00043 " ", 00044 " .+++. ", 00045 " .@@@.+ ", 00046 " ..@+++@.. ", 00047 " +@+...+@+ ", 00048 " +@+. +@+. ", 00049 " +@+. +@+. ", 00050 " +++++++++++ ", 00051 " +#########+. ", 00052 " +#.......#+. ", 00053 " +#@@@@@@@#+. ", 00054 " +#.......#+. ", 00055 " +#########+. ", 00056 " +++++++++++. ", 00057 " ........... ", 00058 " ", 00059 " ", 00060 " ", 00061 " "}; 00062 00063 /* XPM */ 00064 static const char * const openlock_xpm[] = { 00065 "22 22 5 1", 00066 " c None", 00067 ". c #808080", 00068 "+ c #000000", 00069 "@ c #FFFFFF", 00070 "# c #C0C0C0", 00071 " ", 00072 " ", 00073 " .+++. ", 00074 " .@@@.+ ", 00075 " ..@+++@.. ", 00076 " +@+...+@+ ", 00077 " +@+. +@+. ", 00078 " +@+. +@+. ", 00079 " +++. +@+. ", 00080 " ... +@+. ", 00081 " +@+. ", 00082 " +++++++++++ ", 00083 " +#########+. ", 00084 " +#.......#+. ", 00085 " +#@@@@@@@#+. ", 00086 " +#.......#+. ", 00087 " +#########+. ", 00088 " +++++++++++. ", 00089 " ........... ", 00090 " ", 00091 " ", 00092 " "}; 00093 00094 KAuthIcon::KAuthIcon(TQWidget *parent, const char *name) 00095 : TQWidget(parent, name), 00096 lockPM( const_cast< const char** >( lock_xpm)), 00097 openLockPM( const_cast< const char** >(openlock_xpm)) 00098 { 00099 lockText = i18n("Editing disabled"); 00100 openLockText = i18n("Editing enabled"); 00101 00102 lockBox = new TQLabel(this); 00103 lockBox->setFrameStyle(TQFrame::WinPanel|TQFrame::Raised); 00104 lockBox->setPixmap(lockPM); 00105 lockBox->setFixedSize(lockBox->sizeHint()); 00106 00107 lockLabel = new TQLabel(this); 00108 lockLabel->setFrameStyle(TQFrame::NoFrame); 00109 00110 // set fixed size of this frame to whichever phrase is longer 00111 if (lockLabel->fontMetrics().boundingRect(lockText).width() > 00112 lockLabel->fontMetrics().boundingRect(openLockText).width()) 00113 lockLabel->setText(lockText); 00114 else 00115 lockLabel->setText(openLockText); 00116 lockLabel->setAlignment(AlignCenter); 00117 lockLabel->setMinimumSize(lockLabel->sizeHint()); 00118 lockLabel->setText(lockText); 00119 00120 layout = new TQHBoxLayout(this); 00121 00122 layout->addWidget(lockBox, 0, AlignLeft|AlignVCenter); 00123 layout->addSpacing(5); 00124 layout->addWidget(lockLabel, 0, AlignRight|AlignVCenter); 00125 00126 layout->activate(); 00127 resize(sizeHint()); 00128 } 00129 00130 KAuthIcon::~KAuthIcon() 00131 { 00132 } 00133 00134 00135 TQSize KAuthIcon::sizeHint() const 00136 { 00137 return layout->minimumSize(); 00138 } 00139 00140 00141 /************************************************************************/ 00142 00143 KRootPermsIcon::KRootPermsIcon(TQWidget *parent, const char *name) 00144 : KAuthIcon(parent, name) 00145 { 00146 updateStatus(); 00147 } 00148 00149 00150 KRootPermsIcon::~KRootPermsIcon() 00151 { 00152 } 00153 00154 void KRootPermsIcon::updateStatus() 00155 { 00156 const bool newRoot = (geteuid() == 0); 00157 lockBox->setPixmap(newRoot ? openLockPM : lockPM); 00158 lockLabel->setText(newRoot ? openLockText : lockText); 00159 update(); 00160 if (root != newRoot) { 00161 root = newRoot; 00162 emit authChanged(newRoot); 00163 } 00164 } 00165 00166 /************************************************************************/ 00167 00168 KWritePermsIcon::KWritePermsIcon(const TQString & fileName, 00169 TQWidget *parent, const char *name) 00170 : KAuthIcon(parent, name) 00171 { 00172 fi.setFile(fileName); 00173 updateStatus(); 00174 } 00175 00176 00177 KWritePermsIcon::~KWritePermsIcon() 00178 { 00179 } 00180 00181 void KWritePermsIcon::updateStatus() 00182 { 00183 bool newwrite; 00184 newwrite = fi.isWritable(); 00185 lockBox->setPixmap(newwrite ? openLockPM : lockPM); 00186 lockLabel->setText(newwrite ? openLockText : lockText); 00187 update(); 00188 if (writable != newwrite) { 00189 writable = newwrite; 00190 emit authChanged(newwrite); 00191 } 00192 } 00193 00194 void KAuthIcon::virtual_hook( int, void* ) 00195 { /*BASE::virtual_hook( id, data );*/ } 00196 00197 void KRootPermsIcon::virtual_hook( int id, void* data ) 00198 { KAuthIcon::virtual_hook( id, data ); } 00199 00200 void KWritePermsIcon::virtual_hook( int id, void* data ) 00201 { KAuthIcon::virtual_hook( id, data ); } 00202 00203 #include "kauthicon.moc"