~kubuntu-kdesudo/kdesudo/trunk

« back to all changes in this revision

Viewing changes to kdesudo/main.cpp

  • Committer: Anthony Mercatante
  • Date: 2008-01-24 19:12:18 UTC
  • Revision ID: tonio@kubuntu-20080124191218-phiplqqua2azkzud
Various fixes, see changelog for details

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#include <kstandarddirs.h>
32
32
#include <kprocess.h>
33
33
#include <kdebug.h>
34
 
#include <qstring.h>
35
34
#include <qfile.h>
36
35
#include <qfileinfo.h>
37
36
 
50
49
        { "p <priority>", I18N_NOOP("Process priority, between 0 and 100, 0 the lowest [50]"), 0},
51
50
        { "r", I18N_NOOP("Use realtime scheduling"), 0},
52
51
        { "f <file>", I18N_NOOP("Use target UID if <file> is not writeable"), 0},
53
 
        { "t", I18N_NOOP("Enable terminal output (no password keeping)"), 0 },
 
52
        { "t", I18N_NOOP("Fake option for KDE's KdeSu compatibility"), 0 },
54
53
        { "n", I18N_NOOP("Do not keep password"), 0},
55
54
        { "nonewdcop", I18N_NOOP("Use existing DCOP server"), 0},
56
55
        { "comment <dialog text>", I18N_NOOP("The comment that should be displayed in the dialog"),  0},
63
62
{
64
63
        KAboutData aboutData("kdesudo", I18N_NOOP("KdeSudo"),
65
64
                VERSION, description, KAboutData::License_GPL,
66
 
                "(c) 2003, Robert Gruber", 0, 0, "rgruber@users.sourceforge.net");
 
65
                "(c) 2007-2008, Anthony Mercatante", 0, 0, "tonio@ubuntu.com");
67
66
        aboutData.addAuthor("Robert Gruber",0, "rgruber@users.sourceforge.net");
 
67
        aboutData.addAuthor("Anthony Mercatante",0, "tonio@ubuntu.com");
68
68
        KCmdLineArgs::init(argc, argv, &aboutData);
69
69
        KCmdLineArgs::addCmdLineOptions(options); // Add our own options.
70
70
 
71
71
        KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
72
 
        /* Get the comment out of cli args */
73
 
        QByteArray commentBytes = args->getOption("comment");
74
 
        QTextCodec* tCodecConv = QTextCodec::codecForLocale();
75
 
        QString commentArg = tCodecConv->toUnicode(commentBytes, commentBytes.size());
76
72
 
77
73
        KApplication a;
78
74
 
79
 
        QString strRunas = args->getOption("u");
80
 
 
81
75
        QString strCmd;
82
76
        QString executable;
83
77
        QString arg;
84
78
        QString command;
85
79
        QStringList commandlist;
86
 
        QString priority;
87
 
        bool showPriority = false;
88
 
        bool cleanCachedPwd = false;
89
 
        bool changeUID = true;
90
 
        bool useTerminal = false;
91
 
        bool keepPwd = true;
 
80
        QString icon;
92
81
 
93
82
        bool withIgnoreButton = args->isSet("ignorebutton");
94
83
 
95
 
        /* forget passwords */
96
 
        if (args->isSet("s")) 
97
 
                cleanCachedPwd = true;
98
 
 
99
 
        if (args->isSet("n"))
100
 
                keepPwd = false;
101
 
 
102
 
        /* -i icon parameter */
103
 
        QString icon = "";
104
 
        if (args->isSet("i"))
105
 
                icon = args->getOption("i");
106
 
 
107
 
        /* command prompt handling */
108
 
        bool showCommand = true;
109
 
        if (args->isSet("d"))
110
 
                showCommand = false;
111
 
 
112
 
        if (args->isSet("r"))
113
 
        {
114
 
                strCmd = QString("nice -n 10");
115
 
 
116
 
                showPriority = true;
117
 
                priority = i18n("realtime:") + QChar(' ') + QString("50/100");
118
 
        }
119
 
        else if (args->isSet("p"))
120
 
        {
121
 
                QString n = args->getOption("n");
122
 
                int intn = atoi(n);
123
 
                intn =  (intn * 40 / 100) - (20 + 0.5);
124
 
        
125
 
                QString strn;
126
 
                strn.sprintf("%d",intn);
127
 
                strCmd = KProcess::quote("nice") + QChar(' ') + KProcess::quote("-n") + QChar(' ') + KProcess::quote(strn);
128
 
 
129
 
                showPriority = true;
130
 
                priority = n + QString("/100");
131
 
        }
132
 
 
133
 
        if (args->isSet("c"))
134
 
        {
135
 
                command = args->getOption("c");
136
 
                commandlist = QStringList::split(" ", command);
137
 
                executable = commandlist[0];
138
 
                strCmd = command;
139
 
        }
140
 
 
141
 
        if (args->count()==1)
 
84
        if (args->count())
142
85
        {
143
86
                if (executable.isEmpty())
144
87
                {
146
89
                        commandlist = QStringList::split(QChar(' '), command);
147
90
                        executable = commandlist[0];
148
91
                }
149
 
 
150
 
                strCmd = command;
151
 
        }
152
 
        else if (args->count())
153
 
        {
154
 
                for (int i = 0; i < args->count(); i++)
155
 
                {
156
 
                        command = args->arg(i);
157
 
                        if (executable.isEmpty())
158
 
                        {
159
 
                                commandlist = QStringList::split(" ", command);
160
 
                                executable = commandlist[0];
161
 
                        }
162
 
 
163
 
                        strCmd += QChar(' ') + KProcess::quote(command);
164
 
                }
165
 
        }
166
 
 
167
 
        strCmd = strCmd.stripWhiteSpace();
168
 
 
169
 
        if (args->isSet("f"))
170
 
        {
171
 
                // If file is writeable, do not change uid
172
 
                QString filename = QFile::decodeName(args->getOption("f"));
173
 
                QString file = filename;
174
 
                if (!file.isEmpty())
175
 
                {
176
 
                        if (file.at(0) != '/')
177
 
                        {
178
 
                                KStandardDirs dirs;
179
 
                                dirs.addKDEDefaults();
180
 
                                file = dirs.findResource("config", file);
181
 
                                if (file.isEmpty())
182
 
                                {
183
 
                                        kdError(1206) << "Config file not found: " << file << "\n";
184
 
                                        exit(1);
185
 
                                }
186
 
                        }
187
 
                        QFileInfo fi(file);
188
 
                        if (!fi.exists())
189
 
                        {
190
 
                                kdError(1206) << "File does not exist: " << file << "\n";
191
 
                                exit(1);
192
 
                        }
193
 
                        if (fi.isWritable())
194
 
                        {
195
 
                                changeUID = false;
196
 
                        }
197
 
                }
198
 
        }
199
 
 
200
 
        if (args->isSet("t"))
201
 
                useTerminal = true;
202
 
 
203
 
        if (strCmd.isEmpty() && cleanCachedPwd==false)
204
 
        {
205
 
                KMessageBox::information(NULL, i18n("No command arguments supplied!\nUsage: kdesudo [-u <runas>] <command>\nKdeSudo will now exit..."));
206
 
                return 0;
207
92
        }
208
93
 
209
94
        /* Kubuntu has a bug in it - this is a workaround for it */  
210
95
        KGlobal::dirs()->addResourceDir("apps","/usr/share/applications/kde");
211
96
        KGlobal::dirs()->addResourceDir("apps","/usr/share/applications");
212
 
        /* icon parsing */
213
97
        QString deskFilePath = KGlobal::dirs()->findResource("apps",executable + ".desktop");
214
98
        KDesktopFile desktopFile(deskFilePath,true);
215
 
        if (icon.isEmpty())
 
99
 
 
100
        /* icon parsing */
 
101
        if (args->isSet("i"))
 
102
                icon = args->getOption("i");
 
103
        else
216
104
        {
217
105
                QString iconName = desktopFile.readIcon();
218
 
                //qDebug(deskFilePath);
219
106
                icon = KGlobal::iconLoader()->iconPath(iconName, -1* KIcon::StdSizes(KIcon::SizeHuge), true);
220
107
        }
 
108
 
221
109
        /* generic name parsing */
222
110
        QString name = desktopFile.readName();
223
111
        QString genericName = desktopFile.readGenericName();
230
118
        else if (!genericName.isEmpty())
231
119
                name = genericName;
232
120
 
233
 
        bool newDcop = args->isSet("newdcop");
234
 
 
235
 
        KdeSudo *kdesudo = new KdeSudo(0,0,strCmd,strRunas,commentArg,icon,name,withIgnoreButton,newDcop,cleanCachedPwd,changeUID,useTerminal,keepPwd);
236
 
 
237
 
        if(showCommand==true)
238
 
                (*kdesudo).addLine(i18n("Command:"), strCmd);
239
 
 
240
 
        if((showPriority==true) && (priority!=QString()))
241
 
                (*kdesudo).addLine(i18n("Priority:"), priority);
 
121
        KdeSudo *kdesudo = new KdeSudo(0,0,icon,name,withIgnoreButton);
242
122
 
243
123
        a.setMainWidget(kdesudo);
244
124
        args->clear();