Following the discussion on kde-devel: this is a VERY cleaned up version of

kdialog (based on kdialog-kde3-0.2.1, but I removed the client/server stuff,
the 'width height' arguments, fixed all the encoding problems, used
the KDE widgets like KLineEditDlg and KPasswordDialog, etc.)

New features include: many messageboxes from KMessageBox (--yesnocancel,
--warningyesno, --error etc.)

Now you have a very easy way to create KDE dialog boxes from shell scripts.

svn path=/trunk/kdebase/kdialog/; revision=167164
This commit is contained in:
David Faure 2002-07-17 14:21:33 +00:00
parent 91ea8ded1a
commit 71811b6127
8 changed files with 614 additions and 0 deletions

13
kdialog/Makefile.am Normal file
View file

@ -0,0 +1,13 @@
KDE_CXXFLAGS = -DQT_NO_CAST_ASCII -DQT_NO_ASCII_CAST
INCLUDES = $(all_includes)
bin_PROGRAMS = kdialog
kdialog_SOURCES = kdialog.cpp widgets.cpp klistboxdialog.cpp
kdialog_LDADD = $(LIB_KDEUI)
kdialog_LDFLAGS = $(all_libraries) $(KDE_RPATH)
METASOURCES = AUTO
messages:
$(XGETTEXT) $(kdialog_SOURCES) -o $(podir)/kdialog.pot

8
kdialog/README Normal file
View file

@ -0,0 +1,8 @@
kdialog allows to display dialog boxes from shell scripts.
The syntax is very much inspired from the "dialog" command
(which shows text mode dialogs).
However the width and height attributes have been removed for
most dialogs - Qt/KDE have layouts ;)
Current maintainer: David Faure <faure@kde.org>

262
kdialog/kdialog.cpp Normal file
View file

@ -0,0 +1,262 @@
//
// Copyright (C) 1998 Matthias Hoelzer <hoelzer@kde.org>
// Copyright (C) 2002 David Faure <faure@kde.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the7 implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#include <fcntl.h>
#include <unistd.h>
#include <stream.h>
#include <stdlib.h>
#include <qlist.h>
#include <qfile.h>
#include <qdstream.h>
#include <qregexp.h>
#include <kmessagebox.h>
#include <kapp.h>
#include <kdebug.h>
//#include <ktopwidget.h>
#include <kmainwindow.h>
#include "widgets.h"
#include <klocale.h>
#include <qdialog.h>
#include <kcmdlineargs.h>
#include <kaboutdata.h>
static KCmdLineOptions options[] =
{
{ "yesno <text>", I18N_NOOP("Question message box with yes/no buttons"), 0 },
{ "yesnocancel <text>", I18N_NOOP("Question message box with yes/no/cancel buttons"), 0 },
{ "warningyesno <text>", I18N_NOOP("Warning message box with yes/no buttons"), 0 },
{ "warningcontinuecancel <text>", I18N_NOOP("Warning message box with continue/cancel buttons"), 0 },
{ "warningyesnocancel <text>", I18N_NOOP("Warning message box with yes/no/cancel buttons"), 0 },
{ "sorry <text>", I18N_NOOP("'Sorry' message box"), 0 },
{ "error <text>", I18N_NOOP("'Error' message box"), 0 },
{ "msgbox <text>", I18N_NOOP("Message Box dialog"), 0 },
{ "inputbox <text> <init>", I18N_NOOP("Input Box dialog"), 0 },
{ "password <text>", I18N_NOOP("Password dialog"), 0 },
{ "textbox <file> [width] [height]", I18N_NOOP("Text Box dialog"), 0 },
{ "menu <text> [tag item] [tag item] ...", I18N_NOOP("Menu dialog"), 0 },
{ "checklist <text> [tag item status] ...", I18N_NOOP("Check List dialog"), 0 },
{ "radiolist <text> [tag item status] ...", I18N_NOOP("Radio List dialog"), 0 },
// TODO gauge stuff, reading values from stdin
{ "title <text>", I18N_NOOP("Dialog title"), 0 },
{ "separate-output", I18N_NOOP("Return list items on separate lines (for checklist option)"), 0 },
{ "+[arg]", I18N_NOOP("Arguments - depending on main option"), 0 },
KCmdLineLastOption
};
// string to int, with default value
int convert(const QString &val, int def)
{
bool ok;
int result = val.toInt(&ok);
if (!ok)
result = def;
return result;
}
int directCommand(KCmdLineArgs *args)
{
QString title;
bool separateOutput = FALSE;
// --title text
if (args->isSet("title"))
{
title = QString::fromLocal8Bit(args->getOption("title"));
cout << title.local8Bit().data() << endl;
}
// --separate-output
if (args->isSet("separate-output"))
{
separateOutput = TRUE;
}
// --yesno and other message boxes
KMessageBox::DialogType type = (KMessageBox::DialogType) 0;
QCString option;
if (args->isSet("yesno")) {
option = "yesno";
type = KMessageBox::QuestionYesNo;
}
else if (args->isSet("yesnocancel")) {
option = "yesnocancel";
type = KMessageBox::QuestionYesNoCancel;
}
else if (args->isSet("warningyesno")) {
option = "warningyesno";
type = KMessageBox::WarningYesNo;
}
else if (args->isSet("warningyesnocancel")) {
option = "warningyesnocancel";
type = KMessageBox::WarningYesNoCancel;
}
else if (args->isSet("sorry")) {
option = "sorry";
type = KMessageBox::Sorry;
}
else if (args->isSet("error")) {
option = "error";
type = KMessageBox::Error;
}
else if (args->isSet("msgbox")) {
option = "msgbox";
type = KMessageBox::Information;
}
if ( !option.isEmpty() )
{
QString text = QString::fromLocal8Bit(args->getOption( option ));
int pos;
while ((pos = text.find( QString::fromLatin1("\\n") )) >= 0)
{
text.replace(pos, 2, QString::fromLatin1("\n"));
}
int ret = KMessageBox::messageBox( 0, type, text, title /*, TODO configurable button texts*/ );
// ret is 1 for Ok, 2 for Cancel, 3 for Yes, 4 for No and 5 for Continue.
// We want to return 0 for ok, yes and continue, 1 for no and 2 for cancel
return (ret == KMessageBox::Ok || ret == KMessageBox::Yes || ret == KMessageBox::Continue) ? 0
: ( ret == KMessageBox::No ? 1 : 2 );
}
// --inputbox text [init]
if (args->isSet("inputbox"))
{
QString result;
QString init;
if (args->count() > 0)
init = args->arg(0);
bool retcode = Widgets::inputBox(0, title, QString::fromLocal8Bit(args->getOption("inputbox")), init, result);
cout << result.local8Bit().data() << endl;
return retcode ? 0 : 1;
}
// --password text
if (args->isSet("password"))
{
QCString result;
bool retcode = Widgets::passwordBox(0, title, QString::fromLocal8Bit(args->getOption("password")), result);
cout << result.data() << endl;
return retcode ? 0 : 1;
}
// --textbox file [width] [height]
if (args->isSet("textbox"))
{
int w = 0;
int h = 0;
if (args->count() == 2) {
w = QString::fromLocal8Bit(args->arg(0)).toInt();
h = QString::fromLocal8Bit(args->arg(1)).toInt();
}
return Widgets::textBox(0, w, h, title, QString::fromLocal8Bit(args->getOption("textbox")));
}
// --menu text [tag item] [tag item] ...
if (args->isSet("menu")) {
QStringList list;
if (args->count() >= 2) {
for (int i = 0; i < args->count(); i++) {
list.append(QString::fromLocal8Bit(args->arg(i)));
}
QString text = QString::fromLocal8Bit(args->getOption("menu"));
QString result;
bool retcode = Widgets::listBox(0, title, text, list, result);
cout << result.local8Bit().data() << endl;
return retcode ? 0 : 1;
}
return -1;
}
// --checklist text [tag item status] [tag item status] ...
if (args->isSet("checklist")) {
QStringList list;
if (args->count() >= 3) {
for (int i = 0; i < args->count(); i++) {
list.append(QString::fromLocal8Bit(args->arg(i)));
}
QString text = QString::fromLocal8Bit(args->getOption("checklist"));
QStringList result;
bool retcode = Widgets::checkList(0, title, text, list, separateOutput, result);
unsigned int i;
for (i=0; i<result.count(); i++)
cout << result[i].local8Bit().data() << endl;;
exit( retcode ? 0 : 1 );
}
return -1;
}
// --radiolist text width height menuheight [tag item status]
if (args->isSet("radiolist")) {
QStringList list;
if (args->count() >= 3) {
for (int i = 0; i < args->count(); i++) {
list.append(QString::fromLocal8Bit(args->arg(i)));
}
QString text = QString::fromLocal8Bit(args->getOption("radiolist"));
QString result;
bool retcode = Widgets::radioBox(0, title, text, list, result);
cout << result.local8Bit().data() << endl;
exit( retcode ? 0 : 1 );
}
return -1;
}
KCmdLineArgs::usage();
return -2; // NOTREACHED
}
int main(int argc, char *argv[])
{
KAboutData aboutData( "kdialog", I18N_NOOP("KDialog"),
"0.9", I18N_NOOP( "KDialog can be used to show nice dialog boxes from shell scripts" ), KAboutData::License_BSD,
"(C) 2000, Nick Thompson");
aboutData.addAuthor("David Faure", I18N_NOOP("Current maintainer"),"faure@kde.org");
aboutData.addAuthor("Nick Thompson",0, 0/*"nickthompson@lucent.com" bounces*/);
aboutData.addAuthor("Matthias Hölzer",0,"hoelzer@kde.org");
aboutData.addAuthor("David Gümbel",0,"david.guembel@gmx.net");
KCmdLineArgs::init(argc, argv, &aboutData);
KCmdLineArgs::addCmdLineOptions( options ); // Add our own options.
KApplication app;
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
// execute direct kdialog command
return directCommand(args);
}

View file

@ -0,0 +1,49 @@
//
// Copyright (C) 1998 Matthias Hoelzer
// email: hoelzer@physik.uni-wuerzburg.de
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the7 implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#include <qlabel.h>
#include <qlistbox.h>
#include <qvbox.h>
#include "klistboxdialog.h"
#include "klistboxdialog.moc"
#include "klocale.h"
KListBoxDialog::KListBoxDialog(QString text, QWidget *parent)
: KDialogBase( parent, 0, true, QString::null, Ok|Cancel, Ok, true )
{
QVBox *page = makeVBoxMainWidget();
label = new QLabel(text, page);
label->setAlignment(AlignCenter);
table = new QListBox(page);
}
void KListBoxDialog::insertItem(const QString& item)
{
table->insertItem(item);
table->setCurrentItem(0);
}
int KListBoxDialog::currentItem()
{
return table->currentItem();
}

48
kdialog/klistboxdialog.h Normal file
View file

@ -0,0 +1,48 @@
//
// Copyright (C) 1998 Matthias Hoelzer
// email: hoelzer@physik.uni-wuerzburg.de
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the7 implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#ifndef _KLISTBOXDIALOG_H_
#define _KLISTBOXDIALOG_H_
#include <kdialogbase.h>
class KListBoxDialog : public KDialogBase
{
Q_OBJECT
public:
KListBoxDialog(QString text, QWidget *parent=0);
~KListBoxDialog() {};
QListBox &getTable() { return *table; };
void insertItem( const QString& text );
int currentItem();
protected:
QListBox *table;
QLabel *label;
};
#endif

30
kdialog/test Executable file
View file

@ -0,0 +1,30 @@
echo "yesno box:"
kdialog --title "This is a yesno box" --yesno "Choose: yes or no"
if [ $? = 0 ]; then
echo " your choice was: yes"
else
echo " your choice was: no"
fi
echo "message box:"
kdialog --title "This is a message" --msgbox "Well, this is it:\nthe message"
echo "input box:"
kdialog --title "This is a input box" --inputbox "What is your name" "Joe User"
if [ $? = 0 ]; then
echo " you accepted"
else
echo " you did not accept"
fi
echo "text box:"
kdialog --title "This is a text box" --textbox widgets.h 400 300
echo "menu:"
kdialog --title "This is a menu" --menu "Choose one of these" a English b German c French d Spanish
echo "checklist:"
kdialog --title "This is a checklist" --checklist "Choose some of these" a English on b German off c French off d Spanish on
echo "radiolist:"
kdialog --title "This is a radiolist" --radiolist "Choose one of these" a English off b German off c French on d Spanish off

167
kdialog/widgets.cpp Normal file
View file

@ -0,0 +1,167 @@
//
// Copyright (C) 1998 Matthias Hoelzer <hoelzer@kde.org>
// Copyright (C) 2002 David Faure <faure@kde.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the7 implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#include <klocale.h>
#include <kmessagebox.h>
#include "widgets.h"
#include "klistboxdialog.h"
#include <klineeditdlg.h>
#include <kpassdlg.h>
#include <kdebug.h>
#include <qtextedit.h>
#include <qvbox.h>
#include <qfile.h>
bool Widgets::inputBox(QWidget *parent, const QString& title, const QString& text, const QString& init, QString &result)
{
KLineEditDlg dlg( text, init, parent );
dlg.setCaption(title);
bool retcode = (dlg.exec() == QDialog::Accepted);
result = dlg.text();
return retcode;
}
bool Widgets::passwordBox(QWidget *parent, const QString& title, const QString& text, QCString &result)
{
KPasswordDialog dlg( KPasswordDialog::Password, false, 0, parent );
dlg.setCaption(title);
dlg.setPrompt(text);
bool retcode = (dlg.exec() == QDialog::Accepted);
result = dlg.password();
return retcode;
}
int Widgets::textBox(QWidget *parent, int width, int height, const QString& title, const QString& file)
{
// KTextBox dlg(parent, 0, TRUE, width, height, file);
KDialogBase dlg( parent, 0, true, QString::null, KDialogBase::Ok, KDialogBase::Ok );
dlg.setCaption(title);
QTextEdit *edit = new QTextEdit( dlg.makeVBoxMainWidget() );
edit->setReadOnly(TRUE);
QFile f(file);
if (!f.open(IO_ReadOnly))
{
kdError() << i18n("kdialog: could not open file ") << file << endl;
return -1;
}
QTextStream s(&f);
while (!s.eof())
edit->append(s.readLine());
f.close();
if ( width > 0 && height > 0 )
dlg.setInitialSize( QSize( width, height ) );
dlg.exec();
return 0;
}
bool Widgets::listBox(QWidget *parent, const QString& title, const QString& text, const QStringList& args, QString &result)
{
KListBoxDialog box(text,parent);
box.setCaption(title);
for (unsigned int i = 0; i+1<args.count(); i += 2) {
box.insertItem(args[i+1]);
}
bool retcode = (box.exec() == QDialog::Accepted);
result = args[ box.currentItem()*2 ];
return retcode;
}
bool Widgets::checkList(QWidget *parent, const QString& title, const QString& text, const QStringList& args, bool separateOutput, QStringList &result)
{
QStringList entries, tags;
QString rs;
result.clear();
KListBoxDialog box(text,parent);
QListBox &table = box.getTable();
box.setCaption(title);
for (unsigned int i=0; i+2<args.count(); i += 3) {
tags.append(args[i]);
entries.append(args[i+1]);
}
table.insertStringList(entries);
table.setMultiSelection(TRUE);
table.setCurrentItem(0); // This is to circumvent a Qt bug
for (unsigned int i=0; i+2<args.count(); i += 3) {
table.setSelected( i/3, args[i+2] == QString::fromLatin1("on") );
}
bool retcode = (box.exec() == QDialog::Accepted);
if (separateOutput) {
for (unsigned int i=0; i<table.count(); i++)
if (table.isSelected(i))
result.append(tags[i]);
} else {
for (unsigned int i=0; i<table.count(); i++)
if (table.isSelected(i))
rs += QString::fromLatin1("\"") + tags[i] + QString::fromLatin1("\" ");
result.append(rs);
}
return retcode;
}
bool Widgets::radioBox(QWidget *parent, const QString& title, const QString& text, const QStringList& args, QString &result)
{
QStringList entries, tags;
KListBoxDialog box(text,parent);
QListBox &table = box.getTable();
box.setCaption(title);
for (unsigned int i=0; i+2<args.count(); i += 3) {
tags.append(args[i]);
entries.append(args[i+1]);
}
table.insertStringList(entries);
for (unsigned int i=0; i+2<args.count(); i += 3) {
table.setSelected( i/3, args[i+2] == QString::fromLatin1("on") );
}
bool retcode = (box.exec() == QDialog::Accepted);
result = tags[ table.currentItem() ];
return retcode;
}

37
kdialog/widgets.h Normal file
View file

@ -0,0 +1,37 @@
//
// Copyright (C) 1998 Matthias Hoelzer <hoelzer@kde.org>
// Copyright (C) 2002 David Faure <faure@kde.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the7 implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#ifndef _WIDGETS_H_
#define _WIDGETS_H_
#include <qwidget.h>
#include <qstring.h>
namespace Widgets
{
bool inputBox(QWidget *parent, const QString& title, const QString& text, const QString& init, QString &result);
bool passwordBox(QWidget *parent, const QString& title, const QString& text, QCString &result);
int textBox(QWidget *parent, int width, int height, const QString& title, const QString& file);
bool listBox(QWidget *parent, const QString& title, const QString& text, const QStringList& args, QString &result);
bool checkList(QWidget *parent, const QString& title, const QString& text, const QStringList& args, bool separateOutput, QStringList &result);
bool radioBox(QWidget *parent, const QString& title, const QString& text, const QStringList& args, QString &result);
};
#endif