Proof-reading - fixed one usage of the i18n plural form (it wasn't doing before,...
[kdeadmin.git] / ksystemlog / src / loggerDialog.cpp
blobcbe1cf5f3fca5669180a819a68c2b6fc0b6d258a
1 /***************************************************************************
2 * KSystemLog, a system log viewer tool *
3 * Copyright (C) 2007 by Nicolas Ternisien *
4 * nicolas.ternisien@gmail.com *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 ***************************************************************************/
22 #include "loggerDialog.h"
24 #include <QProcess>
26 #include <klocale.h>
27 #include <kmessagebox.h>
29 //Project includes
30 #include "logging.h"
32 #include "logLevel.h"
33 #include "globals.h"
35 class LoggerDialogPrivate {
36 public:
37 QMap<QString, QString> facilities;
39 QMap<QString, QString> priorities;
40 QMap<QString, QPixmap> priorityIcons;
44 LoggerDialog::LoggerDialog(QWidget *parent) :
45 QDialog(parent),
46 d(new LoggerDialogPrivate()) {
48 setupUi(this);
50 connect(buttonOK, SIGNAL(clicked()), this, SLOT(sendMessage()));
51 connect(buttonCancel, SIGNAL(clicked()), this, SLOT(hide()));
53 connect(tagActivation, SIGNAL(toggled(bool)), this, SLOT(changeTagActivation(bool)));
54 connect(fileActivation, SIGNAL(toggled(bool)), this, SLOT(changeFileActivation(bool)));
55 connect(messageActivation, SIGNAL(toggled(bool)), this, SLOT(changeMessageActivation(bool)));
58 connect(file, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged()));
59 connect(message, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged()));
60 connect(tag, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged()));
62 buildMaps();
64 //Fill the priority ComboBox
65 QList<QString> prioKeys(d->priorities.keys());
67 QList<QString>::Iterator itPriority;
68 for (itPriority=prioKeys.begin(); itPriority!=prioKeys.end(); ++itPriority) {
69 priority->addItem(d->priorityIcons[*itPriority], *itPriority);
73 //Select the right priority
74 for (int i=0; i<priority->count(); ++i) {
75 if (priority->itemText(i)==Globals::instance()->noticeLogLevel()->name()) {
76 priority->setCurrentIndex(i);
77 break;
81 //Fill the priority ComboBox
82 QList<QString> keys(d->facilities.keys());
84 QList<QString>::Iterator itFacility;
85 for (itFacility=keys.begin(); itFacility!=keys.end(); ++itFacility) {
86 facility->addItem(*itFacility);
89 //Select the right facility
90 for (int i=0; i<facility->count(); ++i) {
91 if (facility->itemText(i)==i18n("User")) {
92 facility->setCurrentIndex(i);
93 break;
99 LoggerDialog::~LoggerDialog() {
100 delete d;
103 void LoggerDialog::initialize() {
104 logDebug() << "Initializing Logger dialog..." << endl;
106 message->clear();
107 message->setFocus();
110 void LoggerDialog::buildMaps() {
112 //Fill the facility map
113 d->facilities[i18n("Authentication")]="auth";
114 d->facilities[i18n("Private Authentication")]="authpriv";
115 d->facilities[i18n("Cron")]="cron";
116 d->facilities[i18n("Daemon")]="daemon";
117 d->facilities[i18n("FTP")]="ftp";
118 d->facilities[i18n("Kernel")]="kern";
119 d->facilities[i18n("LPR")]="lpr";
120 d->facilities[i18n("Mail")]="mail";
121 d->facilities[i18n("News")]="news";
122 d->facilities[i18n("Syslog")]="syslog";
123 d->facilities[i18n("User")]="user";
124 d->facilities[i18n("UUCP")]="uucp";
126 d->facilities[i18n("Local 0")]="local0";
127 d->facilities[i18n("Local 1")]="local1";
128 d->facilities[i18n("Local 2")]="local2";
129 d->facilities[i18n("Local 3")]="local3";
130 d->facilities[i18n("Local 4")]="local4";
131 d->facilities[i18n("Local 5")]="local5";
132 d->facilities[i18n("Local 6")]="local6";
133 d->facilities[i18n("Local 7")]="local7";
135 //Fill the priority map
136 d->priorities[Globals::instance()->debugLogLevel()->name()]="debug";
137 d->priorities[Globals::instance()->informationLogLevel()->name()]="info";
138 d->priorities[Globals::instance()->noticeLogLevel()->name()]="notice";
139 d->priorities[Globals::instance()->warningLogLevel()->name()]="warning";
140 d->priorities[Globals::instance()->errorLogLevel()->name()]="err";
141 d->priorities[Globals::instance()->criticalLogLevel()->name()]="crit";
142 d->priorities[Globals::instance()->alertLogLevel()->name()]="alert";
143 d->priorities[Globals::instance()->emergencyLogLevel()->name()]="emerg";
145 //Fill the priority icon map
146 d->priorityIcons[Globals::instance()->debugLogLevel()->name()]=Globals::instance()->debugLogLevel()->icon();
147 d->priorityIcons[Globals::instance()->informationLogLevel()->name()]=Globals::instance()->informationLogLevel()->icon();
148 d->priorityIcons[Globals::instance()->noticeLogLevel()->name()]=Globals::instance()->noticeLogLevel()->icon();
149 d->priorityIcons[Globals::instance()->warningLogLevel()->name()]=Globals::instance()->warningLogLevel()->icon();
150 d->priorityIcons[Globals::instance()->errorLogLevel()->name()]=Globals::instance()->errorLogLevel()->icon();
151 d->priorityIcons[Globals::instance()->criticalLogLevel()->name()]=Globals::instance()->criticalLogLevel()->icon();
152 d->priorityIcons[Globals::instance()->alertLogLevel()->name()]=Globals::instance()->alertLogLevel()->icon();
153 d->priorityIcons[Globals::instance()->emergencyLogLevel()->name()]=Globals::instance()->emergencyLogLevel()->icon();
157 void LoggerDialog::textChanged() {
158 if (fileActivation->isChecked() && file->url().isEmpty()) {
159 buttonOK->setEnabled(false);
160 return;
163 if (tagActivation->isChecked() && tag->text().isEmpty()) {
164 buttonOK->setEnabled(false);
165 return;
168 if (messageActivation->isChecked() && message->text().isEmpty()) {
169 buttonOK->setEnabled(false);
170 return;
173 buttonOK->setEnabled(true);
177 void LoggerDialog::changeTagActivation(bool activation) {
178 tag->setEnabled(activation);
180 textChanged();
183 void LoggerDialog::changeFileActivation(bool activation) {
184 file->setEnabled(activation);
186 textChanged();
189 void LoggerDialog::changeMessageActivation(bool activation) {
190 message->setEnabled(activation);
192 textChanged();
196 void LoggerDialog::sendMessage() {
198 QProcess process;
200 QStringList arguments;
202 if (useProcessIdentifier->isChecked()) {
203 arguments << "-i";
206 if (tagActivation->isChecked()) {
207 arguments << "-t";
209 arguments << tag->text();
212 QString prioritySelected=priority->currentText();
214 if (prioritySelected!=Globals::instance()->noLogLevel()->name()) {
215 arguments << "-p";
217 QString p(d->facilities[facility->currentText()]);
218 p+='.';
219 p+=d->priorities[priority->currentText()];
221 arguments << p;
224 //If we read the content of a file
225 if (fileActivation->isChecked()) {
226 arguments << "-f";
228 arguments << file->url().path();
230 //Else, the user types the content of its message
231 else {
232 //Remove bad "\n" characters
233 arguments << message->text().replace("\n", " ");
236 // QProcess::Block, QProcess::Stdout
237 process.start("logger", arguments);
239 //If the launching of the command failed
240 if (process.error() == QProcess::FailedToStart) {
241 KMessageBox::error(this, i18n("Unable to find the 'logger' command on your system. Please type 'logger' in a Konsole to determine whether this command is installed."), i18n("Command not found"));
242 return;
245 if (process.exitStatus() == QProcess::CrashExit) {
246 KMessageBox::error(this, i18n("The 'logger' command has not been properly exited."), i18n("Execution problem"));
247 return;
250 //No such file or directory
251 if (process.exitCode()==1) {
252 KMessageBox::error(this, i18n("This file does not exist, please choose another."), i18n("File not valid"));
253 return;
256 //Hide the Logger Dialog
257 hide();
261 #include "loggerDialog.moc"