2002-07-17 14:21:33 +00:00
//
// Copyright (C) 1998 Matthias Hoelzer <hoelzer@kde.org>
// Copyright (C) 2002 David Faure <faure@kde.org>
2005-07-03 10:27:34 +00:00
// Copyright (C) 2005 Brad Hards <bradh@frogmouth.net>
2002-07-17 14:21:33 +00:00
//
// 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
2005-10-05 08:44:32 +00:00
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2002-07-17 14:21:33 +00:00
//
2008-01-14 20:20:16 +00:00
# include "widgets.h"
2002-07-17 14:21:33 +00:00
# include <kmessagebox.h>
2002-12-04 14:59:18 +00:00
# include <kapplication.h>
2004-04-26 12:32:44 +00:00
# include <kpassivepopup.h>
2005-07-03 10:27:34 +00:00
# include <krecentdocument.h>
2002-07-17 14:21:33 +00:00
# include <kcmdlineargs.h>
# include <kaboutdata.h>
2003-01-23 22:12:14 +00:00
# include <kfiledialog.h>
2004-04-28 11:37:20 +00:00
# include <kicondialog.h>
2005-07-03 10:27:34 +00:00
# include <kdirselectdialog.h>
2008-01-14 20:20:16 +00:00
# include <kcolordialog.h>
2008-08-18 12:53:23 +00:00
# include <kwindowsystem.h>
2008-01-14 20:20:16 +00:00
# include <QtCore/QTimer>
# include <QtGui/QDesktopWidget>
# include <iostream>
2005-07-03 10:27:34 +00:00
# if defined Q_WS_X11 && ! defined K_WS_QTONLY
# include <netwm.h>
# endif
2002-07-17 14:21:33 +00:00
2008-12-09 06:59:31 +00:00
// #ifdef Q_WS_WIN
// #include <QtGui/QFileDialog>
// #endif
2008-03-26 00:02:59 +00:00
# include <unistd.h>
2008-01-15 20:08:37 +00:00
2003-05-17 09:20:08 +00:00
using namespace std ;
2003-08-16 15:11:45 +00:00
// this class hooks into the eventloop and outputs the id
// of shown dialogs or makes the dialog transient for other winids.
// Will destroy itself on app exit.
class WinIdEmbedder : public QObject
{
public :
2006-04-09 22:10:52 +00:00
WinIdEmbedder ( bool printID , WId winId ) :
2005-07-28 14:59:42 +00:00
QObject ( kapp ) , print ( printID ) , id ( winId )
2003-08-16 15:11:45 +00:00
{
2005-07-28 14:59:42 +00:00
if ( kapp )
kapp - > installEventFilter ( this ) ;
2003-08-16 15:11:45 +00:00
}
protected :
bool eventFilter ( QObject * o , QEvent * e ) ;
private :
bool print ;
WId id ;
} ;
bool WinIdEmbedder : : eventFilter ( QObject * o , QEvent * e )
{
if ( e - > type ( ) = = QEvent : : Show & & o - > isWidgetType ( )
2003-08-18 11:46:23 +00:00
& & o - > inherits ( " KDialog " ) )
2003-08-16 15:11:45 +00:00
{
2003-08-18 11:46:23 +00:00
QWidget * w = static_cast < QWidget * > ( o ) ;
if ( print )
cout < < " winId: " < < w - > winId ( ) < < endl ;
2006-04-09 22:13:07 +00:00
if ( id )
2008-08-18 12:53:23 +00:00
KWindowSystem : : setMainWindow ( w , id ) ;
2006-04-09 22:13:49 +00:00
deleteLater ( ) ; // WinIdEmbedder is not needed anymore after the first dialog was shown
2006-04-09 22:10:52 +00:00
return false ;
2003-08-16 15:11:45 +00:00
}
return QObject : : eventFilter ( o , e ) ;
}
2007-05-13 17:47:20 +00:00
static void outputStringList ( const QStringList & list , bool separateOutput )
2004-08-28 13:10:26 +00:00
{
if ( separateOutput ) {
2007-05-13 17:47:20 +00:00
for ( QStringList : : ConstIterator it = list . begin ( ) ; it ! = list . end ( ) ; + + it ) {
2006-03-17 14:53:55 +00:00
cout < < ( * it ) . toLocal8Bit ( ) . data ( ) < < endl ;
2004-08-28 13:10:26 +00:00
}
} else {
2007-05-13 17:47:20 +00:00
for ( QStringList : : ConstIterator it = list . begin ( ) ; it ! = list . end ( ) ; + + it ) {
2006-03-17 14:53:55 +00:00
cout < < ( * it ) . toLocal8Bit ( ) . data ( ) < < " " ;
2004-08-28 13:10:26 +00:00
}
cout < < endl ;
}
}
2004-04-28 11:37:20 +00:00
static int directCommand ( KCmdLineArgs * args )
2002-07-17 14:21:33 +00:00
{
QString title ;
2005-09-20 10:04:09 +00:00
bool separateOutput = false ;
2003-08-16 15:11:45 +00:00
bool printWId = args - > isSet ( " print-winid " ) ;
2004-08-13 23:43:49 +00:00
QString defaultEntry ;
2002-07-17 14:21:33 +00:00
2003-04-09 11:05:48 +00:00
// --title text
KCmdLineArgs * qtargs = KCmdLineArgs : : parsedArgs ( " qt " ) ; // --title is a qt option
if ( qtargs - > isSet ( " title " ) ) {
2007-07-02 08:49:20 +00:00
title = qtargs - > getOption ( " title " ) ;
2002-07-17 14:21:33 +00:00
}
// --separate-output
if ( args - > isSet ( " separate-output " ) )
{
2005-09-20 10:04:09 +00:00
separateOutput = true ;
2002-07-17 14:21:33 +00:00
}
2008-08-31 12:42:25 +00:00
WId winid = 0 ;
bool attach = args - > isSet ( " attach " ) ;
if ( attach ) {
2008-09-02 16:44:07 +00:00
# ifdef Q_WS_WIN
2008-09-02 15:10:04 +00:00
winid = reinterpret_cast < WId > ( args - > getOption ( " attach " ) . toLong ( & attach , 0 ) ) ; //C style parsing. If the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.
2008-09-02 16:44:07 +00:00
# else
winid = args - > getOption ( " attach " ) . toLong ( & attach , 0 ) ; //C style parsing. If the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.
# endif
2008-08-31 12:42:25 +00:00
} else if ( args - > isSet ( " embed " ) ) {
/* KDialog originally used --embed for attaching the dialog box. However this is misleading and so we changed to --attach.
* For consistancy , we silently map - - embed to - - attach */
attach = true ;
2008-09-02 16:44:07 +00:00
# ifdef Q_WS_WIN
2008-09-02 15:10:04 +00:00
winid = reinterpret_cast < WId > ( args - > getOption ( " embed " ) . toLong ( & attach , 0 ) ) ; //C style parsing. If the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.
2008-09-02 16:44:07 +00:00
# else
winid = args - > getOption ( " embed " ) . toLong ( & attach , 0 ) ; //C style parsing. If the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.
# endif
2008-08-31 12:42:25 +00:00
}
if ( printWId | | attach )
2003-08-16 15:11:45 +00:00
{
2008-08-31 12:42:25 +00:00
( void ) new WinIdEmbedder ( printWId , winid ) ;
2003-08-16 15:11:45 +00:00
}
2002-07-17 14:21:33 +00:00
// --yesno and other message boxes
KMessageBox : : DialogType type = ( KMessageBox : : DialogType ) 0 ;
2005-09-27 07:30:08 +00:00
QByteArray option ;
2002-07-17 14:21:33 +00:00
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 ;
}
2003-08-18 11:46:23 +00:00
else if ( args - > isSet ( " warningcontinuecancel " ) ) {
option = " warningcontinuecancel " ;
type = KMessageBox : : WarningContinueCancel ;
}
2002-07-17 14:21:33 +00:00
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 ( ) )
{
2003-10-23 13:37:30 +00:00
KConfig * dontagaincfg = NULL ;
// --dontagain
2006-01-02 17:28:18 +00:00
QString dontagain ; // QString()
2003-10-23 13:37:30 +00:00
if ( args - > isSet ( " dontagain " ) )
{
QString value = args - > getOption ( " dontagain " ) ;
2006-04-03 17:45:19 +00:00
QStringList values = value . split ( ' : ' , QString : : SkipEmptyParts ) ;
2003-10-23 13:37:30 +00:00
if ( values . count ( ) = = 2 )
{
dontagaincfg = new KConfig ( values [ 0 ] ) ;
KMessageBox : : setDontShowAskAgainConfig ( dontagaincfg ) ;
dontagain = values [ 1 ] ;
}
else
qDebug ( " Incorrect --dontagain! " ) ;
}
2003-08-18 11:46:23 +00:00
int ret ;
2007-07-02 08:49:20 +00:00
QString text = args - > getOption ( option ) ;
2002-07-17 14:21:33 +00:00
int pos ;
2006-04-03 17:45:19 +00:00
while ( ( pos = text . indexOf ( QLatin1String ( " \\ n " ) ) ) > = 0 )
2002-07-17 14:21:33 +00:00
{
2005-09-20 10:04:09 +00:00
text . replace ( pos , 2 , QLatin1String ( " \n " ) ) ;
2002-07-17 14:21:33 +00:00
}
2003-08-18 11:46:23 +00:00
if ( type = = KMessageBox : : WarningContinueCancel ) {
/* TODO configurable button texts*/
2006-12-27 01:38:51 +00:00
ret = KMessageBox : : messageBox ( 0 , type , text , title , KStandardGuiItem : : cont ( ) ,
2007-04-01 11:35:39 +00:00
KStandardGuiItem : : no ( ) , KStandardGuiItem : : cancel ( ) , dontagain ) ;
2003-08-18 11:46:23 +00:00
} else {
2003-10-23 13:37:30 +00:00
ret = KMessageBox : : messageBox ( 0 , type , text , title /*, TODO configurable button texts*/ ,
2007-04-01 11:35:39 +00:00
KStandardGuiItem : : yes ( ) , KStandardGuiItem : : no ( ) , KStandardGuiItem : : cancel ( ) , dontagain ) ;
2003-08-18 11:46:23 +00:00
}
2003-10-23 13:37:30 +00:00
delete dontagaincfg ;
2002-07-17 14:21:33 +00:00
// 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 )
2007-07-02 08:49:20 +00:00
init = args - > arg ( 0 ) ;
2002-07-17 14:21:33 +00:00
2007-07-02 08:49:20 +00:00
bool retcode = Widgets : : inputBox ( 0 , title , args - > getOption ( " inputbox " ) , init , result ) ;
2006-03-17 14:53:55 +00:00
cout < < result . toLocal8Bit ( ) . data ( ) < < endl ;
2002-07-17 14:21:33 +00:00
return retcode ? 0 : 1 ;
}
// --password text
if ( args - > isSet ( " password " ) )
{
2005-09-02 09:00:11 +00:00
QString result ;
2007-07-02 08:49:20 +00:00
bool retcode = Widgets : : passwordBox ( 0 , title , args - > getOption ( " password " ) , result ) ;
2007-08-31 17:47:25 +00:00
cout < < qPrintable ( result ) < < endl ;
2002-07-17 14:21:33 +00:00
return retcode ? 0 : 1 ;
}
2004-04-26 12:32:44 +00:00
// --passivepopup
if ( args - > isSet ( " passivepopup " ) )
{
2004-08-08 20:15:26 +00:00
int duration = 0 ;
if ( args - > count ( ) > 0 )
2007-07-02 08:49:20 +00:00
duration = 1000 * args - > arg ( 0 ) . toInt ( ) ;
2004-08-08 20:15:26 +00:00
if ( duration = = 0 )
duration = 10000 ;
2008-01-14 20:46:51 +00:00
KPassivePopup * popup = KPassivePopup : : message ( KPassivePopup : : Boxed , // style
title ,
args - > getOption ( " passivepopup " ) ,
QPixmap ( ) /* do not crash 0*/ , // icon
( QWidget * ) 0UL , // parent
duration ) ;
KDialog : : centerOnScreen ( popup ) ;
QTimer * timer = new QTimer ( ) ;
QObject : : connect ( timer , SIGNAL ( timeout ( ) ) , kapp , SLOT ( quit ( ) ) ) ;
QObject : : connect ( popup , SIGNAL ( clicked ( ) ) , kapp , SLOT ( quit ( ) ) ) ;
timer - > setSingleShot ( true ) ;
timer - > start ( duration ) ;
2005-07-03 10:27:34 +00:00
2005-09-02 09:00:11 +00:00
# ifdef Q_WS_X11
2005-11-06 15:36:13 +00:00
QString geometry ;
KCmdLineArgs * args = KCmdLineArgs : : parsedArgs ( " kde " ) ;
if ( args & & args - > isSet ( " geometry " ) )
geometry = args - > getOption ( " geometry " ) ;
if ( ! geometry . isEmpty ( ) ) {
2005-07-03 10:27:34 +00:00
int x , y ;
int w , h ;
2006-04-03 17:45:19 +00:00
int m = XParseGeometry ( geometry . toLatin1 ( ) , & x , & y , ( unsigned int * ) & w , ( unsigned int * ) & h ) ;
2005-07-03 10:27:34 +00:00
if ( ( m & XNegative ) )
x = KApplication : : desktop ( ) - > width ( ) + x - w ;
if ( ( m & YNegative ) )
y = KApplication : : desktop ( ) - > height ( ) + y - h ;
popup - > setAnchor ( QPoint ( x , y ) ) ;
}
# endif
2004-04-26 12:32:44 +00:00
kapp - > exec ( ) ;
return 0 ;
}
2002-07-17 14:21:33 +00:00
// --textbox file [width] [height]
if ( args - > isSet ( " textbox " ) )
{
int w = 0 ;
int h = 0 ;
if ( args - > count ( ) = = 2 ) {
2007-07-02 08:49:20 +00:00
w = args - > arg ( 0 ) . toInt ( ) ;
h = args - > arg ( 1 ) . toInt ( ) ;
2002-07-17 14:21:33 +00:00
}
2007-07-02 08:49:20 +00:00
return Widgets : : textBox ( 0 , w , h , title , args - > getOption ( " textbox " ) ) ;
2002-07-17 14:21:33 +00:00
}
2005-01-21 16:57:31 +00:00
// --textinputbox file [width] [height]
2005-01-21 16:53:02 +00:00
if ( args - > isSet ( " textinputbox " ) )
{
int w = 400 ;
int h = 200 ;
if ( args - > count ( ) = = 4 ) {
2007-07-02 08:49:20 +00:00
w = args - > arg ( 2 ) . toInt ( ) ;
h = args - > arg ( 3 ) . toInt ( ) ;
2005-01-21 16:53:02 +00:00
}
QStringList list ;
2007-07-02 08:49:20 +00:00
list . append ( args - > getOption ( " textinputbox " ) ) ;
2005-01-21 16:53:02 +00:00
if ( args - > count ( ) > = 1 ) {
for ( int i = 0 ; i < args - > count ( ) ; i + + )
2007-07-02 08:49:20 +00:00
list . append ( args - > arg ( i ) ) ;
2005-01-21 16:53:02 +00:00
}
2005-09-02 09:00:11 +00:00
QString result ;
2005-01-21 16:53:02 +00:00
int ret = Widgets : : textInputBox ( 0 , w , h , title , list , result ) ;
2007-08-31 17:47:25 +00:00
cout < < qPrintable ( result ) < < endl ;
2005-01-21 16:53:02 +00:00
return ret ;
}
2005-01-21 16:57:31 +00:00
// --combobox <text> [tag item] [tag item] ..."
2003-08-31 01:12:27 +00:00
if ( args - > isSet ( " combobox " ) ) {
QStringList list ;
if ( args - > count ( ) > = 2 ) {
for ( int i = 0 ; i < args - > count ( ) ; i + + ) {
2007-07-02 08:49:20 +00:00
list . append ( args - > arg ( i ) ) ;
2003-08-31 01:12:27 +00:00
}
2007-07-02 08:49:20 +00:00
QString text = args - > getOption ( " combobox " ) ;
2004-08-13 23:43:49 +00:00
if ( args - > isSet ( " default " ) ) {
defaultEntry = args - > getOption ( " default " ) ;
}
2003-08-31 01:12:27 +00:00
QString result ;
2004-08-13 23:43:49 +00:00
bool retcode = Widgets : : comboBox ( 0 , title , text , list , defaultEntry , result ) ;
2006-03-17 14:53:55 +00:00
cout < < result . toLocal8Bit ( ) . data ( ) < < endl ;
2003-08-31 01:12:27 +00:00
return retcode ? 0 : 1 ;
}
return - 1 ;
}
2002-07-17 14:21:33 +00:00
// --menu text [tag item] [tag item] ...
if ( args - > isSet ( " menu " ) ) {
QStringList list ;
if ( args - > count ( ) > = 2 ) {
for ( int i = 0 ; i < args - > count ( ) ; i + + ) {
2007-07-02 08:49:20 +00:00
list . append ( args - > arg ( i ) ) ;
2002-07-17 14:21:33 +00:00
}
2007-07-02 08:49:20 +00:00
QString text = args - > getOption ( " menu " ) ;
2004-08-13 23:43:49 +00:00
if ( args - > isSet ( " default " ) ) {
defaultEntry = args - > getOption ( " default " ) ;
}
2002-07-17 14:21:33 +00:00
QString result ;
2004-08-13 23:43:49 +00:00
bool retcode = Widgets : : listBox ( 0 , title , text , list , defaultEntry , result ) ;
2003-09-01 15:09:50 +00:00
if ( 1 = = retcode ) { // OK was selected
2006-03-17 14:53:55 +00:00
cout < < result . toLocal8Bit ( ) . data ( ) < < endl ;
2003-09-01 15:09:50 +00:00
}
2002-07-17 14:21:33 +00:00
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 + + ) {
2007-07-02 08:49:20 +00:00
list . append ( args - > arg ( i ) ) ;
2002-07-17 14:21:33 +00:00
}
2007-07-02 08:49:20 +00:00
QString text = args - > getOption ( " checklist " ) ;
2002-07-17 14:21:33 +00:00
QStringList result ;
bool retcode = Widgets : : checkList ( 0 , title , text , list , separateOutput , result ) ;
2006-07-22 10:06:56 +00:00
for ( int i = 0 ; i < result . count ( ) ; i + + )
2006-03-17 14:53:55 +00:00
if ( ! result . at ( i ) . toLocal8Bit ( ) . isEmpty ( ) ) {
cout < < result . at ( i ) . toLocal8Bit ( ) . data ( ) < < endl ;
2003-09-01 15:09:50 +00:00
}
2002-07-17 14:21:33 +00:00
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 + + ) {
2007-07-02 08:49:20 +00:00
list . append ( args - > arg ( i ) ) ;
2002-07-17 14:21:33 +00:00
}
2007-07-02 08:49:20 +00:00
QString text = args - > getOption ( " radiolist " ) ;
2002-07-17 14:21:33 +00:00
QString result ;
bool retcode = Widgets : : radioBox ( 0 , title , text , list , result ) ;
2006-03-17 14:53:55 +00:00
cout < < result . toLocal8Bit ( ) . data ( ) < < endl ;
2002-07-17 14:21:33 +00:00
exit ( retcode ? 0 : 1 ) ;
}
return - 1 ;
}
2003-01-23 22:12:14 +00:00
// getopenfilename [startDir] [filter]
if ( args - > isSet ( " getopenfilename " ) ) {
QString startDir ;
QString filter ;
2007-07-02 08:49:20 +00:00
startDir = args - > getOption ( " getopenfilename " ) ;
2003-08-18 11:46:23 +00:00
if ( args - > count ( ) > = 1 ) {
2007-07-02 08:49:20 +00:00
filter = args - > arg ( 0 ) ;
2003-08-18 11:46:23 +00:00
}
2006-02-20 15:27:04 +00:00
KFileDialog dlg ( startDir , filter , 0 ) ;
2005-07-03 10:27:34 +00:00
dlg . setOperationMode ( KFileDialog : : Opening ) ;
if ( args - > isSet ( " multiple " ) ) {
dlg . setMode ( KFile : : Files | KFile : : LocalOnly ) ;
} else {
dlg . setMode ( KFile : : File | KFile : : LocalOnly ) ;
}
Widgets : : handleXGeometry ( & dlg ) ;
kapp - > setTopWidget ( & dlg ) ;
2006-04-26 17:57:02 +00:00
dlg . setCaption ( title . isNull ( ) ? i18n ( " Open " ) : title ) ;
2005-07-03 10:27:34 +00:00
dlg . exec ( ) ;
2005-09-02 09:00:11 +00:00
2004-08-28 13:10:26 +00:00
if ( args - > isSet ( " multiple " ) ) {
2005-07-03 10:27:34 +00:00
QStringList result = dlg . selectedFiles ( ) ;
2004-08-28 13:10:26 +00:00
if ( ! result . isEmpty ( ) ) {
outputStringList ( result , separateOutput ) ;
return 0 ;
}
} else {
2005-07-03 10:27:34 +00:00
QString result = dlg . selectedFile ( ) ;
2004-08-28 13:10:26 +00:00
if ( ! result . isEmpty ( ) ) {
2006-03-17 14:53:55 +00:00
cout < < result . toLocal8Bit ( ) . data ( ) < < endl ;
2004-08-28 13:10:26 +00:00
return 0 ;
}
}
2006-05-31 16:23:36 +00:00
return 1 ; // canceled
2003-01-23 22:12:14 +00:00
}
2005-07-03 10:27:34 +00:00
// getsaveurl [startDir] [filter]
2003-01-23 22:12:14 +00:00
// getsavefilename [startDir] [filter]
2005-07-03 10:27:34 +00:00
if ( ( args - > isSet ( " getsavefilename " ) ) | | ( args - > isSet ( " getsaveurl " ) ) ) {
2003-01-23 22:12:14 +00:00
QString startDir ;
QString filter ;
2005-07-03 10:27:34 +00:00
if ( args - > isSet ( " getsavefilename " ) ) {
2007-07-02 08:49:20 +00:00
startDir = args - > getOption ( " getsavefilename " ) ;
2005-07-03 10:27:34 +00:00
} else {
2007-07-02 08:49:20 +00:00
startDir = args - > getOption ( " getsaveurl " ) ;
2005-07-03 10:27:34 +00:00
}
2003-08-18 11:46:23 +00:00
if ( args - > count ( ) > = 1 ) {
2007-07-02 08:49:20 +00:00
filter = args - > arg ( 0 ) ;
2003-08-18 11:46:23 +00:00
}
2005-07-03 10:27:34 +00:00
// copied from KFileDialog::getSaveFileName(), so we can add geometry
bool specialDir = ( startDir . at ( 0 ) = = ' : ' ) ;
2006-02-20 15:27:04 +00:00
KFileDialog dlg ( specialDir ? startDir : QString ( ) , filter , 0 ) ;
2005-07-03 10:27:34 +00:00
if ( ! specialDir )
dlg . setSelection ( startDir ) ;
dlg . setOperationMode ( KFileDialog : : Saving ) ;
Widgets : : handleXGeometry ( & dlg ) ;
kapp - > setTopWidget ( & dlg ) ;
2006-04-26 17:57:02 +00:00
dlg . setCaption ( title . isNull ( ) ? i18n ( " Save As " ) : title ) ;
2005-07-03 10:27:34 +00:00
dlg . exec ( ) ;
if ( args - > isSet ( " getsaveurl " ) ) {
2006-07-04 20:10:35 +00:00
KUrl result = dlg . selectedUrl ( ) ;
2005-07-03 10:27:34 +00:00
if ( result . isValid ( ) ) {
2006-03-17 14:53:55 +00:00
cout < < result . url ( ) . toLocal8Bit ( ) . data ( ) < < endl ;
2005-07-03 10:27:34 +00:00
return 0 ;
}
} else { // getsavefilename
QString result = dlg . selectedFile ( ) ;
if ( ! result . isEmpty ( ) ) {
KRecentDocument : : add ( result ) ;
2006-03-17 14:53:55 +00:00
cout < < result . toLocal8Bit ( ) . data ( ) < < endl ;
2005-07-03 10:27:34 +00:00
return 0 ;
}
}
2006-05-31 16:23:36 +00:00
return 1 ; // canceled
2003-01-23 22:12:14 +00:00
}
// getexistingdirectory [startDir]
if ( args - > isSet ( " getexistingdirectory " ) ) {
QString startDir ;
2007-07-02 08:49:20 +00:00
startDir = args - > getOption ( " getexistingdirectory " ) ;
2005-07-03 10:27:34 +00:00
QString result ;
# ifdef Q_WS_WIN
2008-12-09 06:59:31 +00:00
result = KFileDialog : : getExistingDirectory ( 0 , title , startDir ,
KFileDialog : : DontResolveSymlinks |
KFileDialog : : ShowDirsOnly ) ;
2005-07-03 10:27:34 +00:00
# else
2006-01-24 08:32:09 +00:00
KUrl url ;
2006-02-20 15:27:04 +00:00
KDirSelectDialog myDialog ( startDir , true , 0 ) ;
2005-07-03 10:27:34 +00:00
kapp - > setTopWidget ( & myDialog ) ;
Widgets : : handleXGeometry ( & myDialog ) ;
if ( ! title . isNull ( ) )
myDialog . setCaption ( title ) ;
if ( myDialog . exec ( ) = = QDialog : : Accepted )
url = myDialog . url ( ) ;
if ( url . isValid ( ) )
result = url . path ( ) ;
# endif
2003-01-23 22:12:14 +00:00
if ( ! result . isEmpty ( ) ) {
2006-03-17 14:53:55 +00:00
cout < < result . toLocal8Bit ( ) . data ( ) < < endl ;
2003-01-23 22:12:14 +00:00
return 0 ;
}
2006-05-31 16:23:36 +00:00
return 1 ; // canceled
2003-01-23 22:12:14 +00:00
}
2003-02-27 21:59:51 +00:00
// getopenurl [startDir] [filter]
if ( args - > isSet ( " getopenurl " ) ) {
QString startDir ;
QString filter ;
2007-07-02 08:49:20 +00:00
startDir = args - > getOption ( " getopenurl " ) ;
2003-08-18 11:46:23 +00:00
if ( args - > count ( ) > = 1 ) {
2007-07-02 08:49:20 +00:00
filter = args - > arg ( 0 ) ;
2003-08-18 11:46:23 +00:00
}
2006-02-20 15:27:04 +00:00
KFileDialog dlg ( startDir , filter , 0 ) ;
2005-07-03 10:27:34 +00:00
dlg . setOperationMode ( KFileDialog : : Opening ) ;
if ( args - > isSet ( " multiple " ) ) {
dlg . setMode ( KFile : : Files ) ;
} else {
dlg . setMode ( KFile : : File ) ;
}
Widgets : : handleXGeometry ( & dlg ) ;
kapp - > setTopWidget ( & dlg ) ;
2006-04-26 17:57:02 +00:00
dlg . setCaption ( title . isNull ( ) ? i18n ( " Open " ) : title ) ;
2005-07-03 10:27:34 +00:00
dlg . exec ( ) ;
2005-09-02 09:00:11 +00:00
2004-08-28 13:10:26 +00:00
if ( args - > isSet ( " multiple " ) ) {
2006-07-04 20:10:35 +00:00
KUrl : : List result = dlg . selectedUrls ( ) ;
2004-08-28 13:10:26 +00:00
if ( ! result . isEmpty ( ) ) {
outputStringList ( result . toStringList ( ) , separateOutput ) ;
return 0 ;
}
} else {
2006-07-04 20:10:35 +00:00
KUrl result = dlg . selectedUrl ( ) ;
2004-08-28 13:10:26 +00:00
if ( ! result . isEmpty ( ) ) {
2006-03-17 14:53:55 +00:00
cout < < result . url ( ) . toLocal8Bit ( ) . data ( ) < < endl ;
2004-08-28 13:10:26 +00:00
return 0 ;
}
}
2006-05-31 16:23:36 +00:00
return 1 ; // canceled
2003-02-27 21:59:51 +00:00
}
2004-04-28 11:37:20 +00:00
// geticon [group] [context]
if ( args - > isSet ( " geticon " ) ) {
QString groupStr , contextStr ;
2007-07-02 08:49:20 +00:00
groupStr = args - > getOption ( " geticon " ) ;
2004-04-28 11:37:20 +00:00
if ( args - > count ( ) > = 1 ) {
2007-07-02 08:49:20 +00:00
contextStr = args - > arg ( 0 ) ;
2004-04-28 11:37:20 +00:00
}
2007-10-01 15:09:41 +00:00
KIconLoader : : Group group = KIconLoader : : NoGroup ;
2005-09-20 10:04:09 +00:00
if ( groupStr = = QLatin1String ( " Desktop " ) )
2007-10-01 15:09:41 +00:00
group = KIconLoader : : Desktop ;
2005-09-20 10:04:09 +00:00
else if ( groupStr = = QLatin1String ( " Toolbar " ) )
2007-10-01 15:09:41 +00:00
group = KIconLoader : : Toolbar ;
2005-09-20 10:04:09 +00:00
else if ( groupStr = = QLatin1String ( " MainToolbar " ) )
2007-10-01 15:09:41 +00:00
group = KIconLoader : : MainToolbar ;
2005-09-20 10:04:09 +00:00
else if ( groupStr = = QLatin1String ( " Small " ) )
2007-10-01 15:09:41 +00:00
group = KIconLoader : : Small ;
2005-09-20 10:04:09 +00:00
else if ( groupStr = = QLatin1String ( " Panel " ) )
2007-10-01 15:09:41 +00:00
group = KIconLoader : : Panel ;
2005-09-20 10:04:09 +00:00
else if ( groupStr = = QLatin1String ( " User " ) )
2007-10-01 15:09:41 +00:00
group = KIconLoader : : User ;
KIconLoader : : Context context = KIconLoader : : Any ;
2004-04-28 11:37:20 +00:00
// From kicontheme.cpp
2005-09-20 10:04:09 +00:00
if ( contextStr = = QLatin1String ( " Devices " ) )
2007-10-01 15:09:41 +00:00
context = KIconLoader : : Device ;
2005-09-20 10:04:09 +00:00
else if ( contextStr = = QLatin1String ( " MimeTypes " ) )
2007-10-01 15:09:41 +00:00
context = KIconLoader : : MimeType ;
2005-09-20 10:04:09 +00:00
else if ( contextStr = = QLatin1String ( " FileSystems " ) )
2007-10-01 15:09:41 +00:00
context = KIconLoader : : FileSystem ;
2005-09-20 10:04:09 +00:00
else if ( contextStr = = QLatin1String ( " Applications " ) )
2007-10-01 15:09:41 +00:00
context = KIconLoader : : Application ;
2005-09-20 10:04:09 +00:00
else if ( contextStr = = QLatin1String ( " Actions " ) )
2007-10-01 15:09:41 +00:00
context = KIconLoader : : Action ;
2004-04-28 11:37:20 +00:00
2006-03-13 10:59:23 +00:00
KIconDialog dlg ( ( QWidget * ) 0L ) ;
2005-07-03 10:27:34 +00:00
kapp - > setTopWidget ( & dlg ) ;
dlg . setup ( group , context ) ;
if ( ! title . isNull ( ) )
dlg . setCaption ( title ) ;
Widgets : : handleXGeometry ( & dlg ) ;
QString result = dlg . openDialog ( ) ;
2004-04-28 11:37:20 +00:00
if ( ! result . isEmpty ( ) ) {
2006-03-17 14:53:55 +00:00
cout < < result . toLocal8Bit ( ) . data ( ) < < endl ;
2004-04-28 11:37:20 +00:00
return 0 ;
}
2006-05-31 16:23:36 +00:00
return 1 ; // canceled
2004-04-28 11:37:20 +00:00
}
2004-06-12 19:52:37 +00:00
// --progressbar text totalsteps
if ( args - > isSet ( " progressbar " ) )
{
2006-10-16 17:56:35 +00:00
cout < < " org.kde.kdialog- " < < getpid ( ) < < " /ProgressDialog " < < endl ;
2004-06-12 19:52:37 +00:00
if ( fork ( ) )
2006-10-16 17:56:35 +00:00
_exit ( 0 ) ;
2004-06-12 19:52:37 +00:00
close ( 1 ) ;
2004-06-14 15:43:35 +00:00
2004-06-12 19:52:37 +00:00
int totalsteps = 100 ;
2007-07-02 08:49:20 +00:00
QString text = args - > getOption ( " progressbar " ) ;
2004-06-12 19:52:37 +00:00
if ( args - > count ( ) = = 1 )
2007-07-02 08:49:20 +00:00
totalsteps = args - > arg ( 0 ) . toInt ( ) ;
2004-06-12 19:52:37 +00:00
return Widgets : : progressBar ( 0 , title , text , totalsteps ) ? 1 : 0 ;
}
2004-06-14 15:43:35 +00:00
2008-01-14 20:20:16 +00:00
// --getcolor
if ( args - > isSet ( " getcolor " ) ) {
KColorDialog dlg ( ( QWidget * ) 0L , true ) ;
if ( args - > isSet ( " default " ) ) {
defaultEntry = args - > getOption ( " default " ) ;
dlg . setColor ( defaultEntry ) ;
}
Widgets : : handleXGeometry ( & dlg ) ;
kapp - > setTopWidget ( & dlg ) ;
dlg . setCaption ( title . isNull ( ) ? i18n ( " Choose Color " ) : title ) ;
if ( dlg . exec ( ) = = KColorDialog : : Accepted ) {
QString result ;
if ( dlg . color ( ) . isValid ( ) ) {
result = dlg . color ( ) . name ( ) ;
} else {
result = dlg . defaultColor ( ) . name ( ) ;
}
cout < < result . toLocal8Bit ( ) . data ( ) < < endl ;
return 0 ;
}
return 1 ; // cancelled
}
2002-07-17 14:21:33 +00:00
KCmdLineArgs : : usage ( ) ;
return - 2 ; // NOTREACHED
}
int main ( int argc , char * argv [ ] )
{
2007-07-02 08:49:20 +00:00
KAboutData aboutData ( " kdialog " , 0 , ki18n ( " KDialog " ) ,
" 1.0 " , ki18n ( " KDialog can be used to show nice dialog boxes from shell scripts " ) ,
2004-09-12 16:12:43 +00:00
KAboutData : : License_GPL ,
2007-07-02 08:49:20 +00:00
ki18n ( " (C) 2000, Nick Thompson " ) ) ;
aboutData . addAuthor ( ki18n ( " David Faure " ) , ki18n ( " Current maintainer " ) , " faure@kde.org " ) ;
aboutData . addAuthor ( ki18n ( " Brad Hards " ) , KLocalizedString ( ) , " bradh@frogmouth.net " ) ;
aboutData . addAuthor ( ki18n ( " Nick Thompson " ) , KLocalizedString ( ) , 0 /*"nickthompson@lucent.com" bounces*/ ) ;
aboutData . addAuthor ( ki18n ( " Matthias Hölzer " ) , KLocalizedString ( ) , " hoelzer@kde.org " ) ;
aboutData . addAuthor ( ki18n ( " David Gümbel " ) , KLocalizedString ( ) , " david.guembel@gmx.net " ) ;
aboutData . addAuthor ( ki18n ( " Richard Moore " ) , KLocalizedString ( ) , " rich@kde.org " ) ;
aboutData . addAuthor ( ki18n ( " Dawit Alemayehu " ) , KLocalizedString ( ) , " adawit@kde.org " ) ;
2008-04-20 23:35:54 +00:00
aboutData . setProgramIconName ( " system-run " ) ;
2002-07-17 14:21:33 +00:00
KCmdLineArgs : : init ( argc , argv , & aboutData ) ;
2007-07-02 08:49:20 +00:00
KCmdLineOptions options ;
options . add ( " yesno <text> " , ki18n ( " Question message box with yes/no buttons " ) ) ;
options . add ( " yesnocancel <text> " , ki18n ( " Question message box with yes/no/cancel buttons " ) ) ;
options . add ( " warningyesno <text> " , ki18n ( " Warning message box with yes/no buttons " ) ) ;
options . add ( " warningcontinuecancel <text> " , ki18n ( " Warning message box with continue/cancel buttons " ) ) ;
options . add ( " warningyesnocancel <text> " , ki18n ( " Warning message box with yes/no/cancel buttons " ) ) ;
options . add ( " sorry <text> " , ki18n ( " 'Sorry' message box " ) ) ;
options . add ( " error <text> " , ki18n ( " 'Error' message box " ) ) ;
options . add ( " msgbox <text> " , ki18n ( " Message Box dialog " ) ) ;
options . add ( " inputbox <text> <init> " , ki18n ( " Input Box dialog " ) ) ;
options . add ( " password <text> " , ki18n ( " Password dialog " ) ) ;
options . add ( " textbox <file> [width] [height] " , ki18n ( " Text Box dialog " ) ) ;
options . add ( " textinputbox <text> <init> [width] [height] " , ki18n ( " Text Input Box dialog " ) ) ;
options . add ( " combobox <text> [tag item] [tag item] ... " , ki18n ( " ComboBox dialog " ) ) ;
options . add ( " menu <text> [tag item] [tag item] ... " , ki18n ( " Menu dialog " ) ) ;
options . add ( " checklist <text> [tag item status] ... " , ki18n ( " Check List dialog " ) ) ;
options . add ( " radiolist <text> [tag item status] ... " , ki18n ( " Radio List dialog " ) ) ;
options . add ( " passivepopup <text> <timeout> " , ki18n ( " Passive Popup " ) ) ;
options . add ( " getopenfilename [startDir] [filter] " , ki18n ( " File dialog to open an existing file " ) ) ;
options . add ( " getsavefilename [startDir] [filter] " , ki18n ( " File dialog to save a file " ) ) ;
options . add ( " getexistingdirectory [startDir] " , ki18n ( " File dialog to select an existing directory " ) ) ;
options . add ( " getopenurl [startDir] [filter] " , ki18n ( " File dialog to open an existing URL " ) ) ;
options . add ( " getsaveurl [startDir] [filter] " , ki18n ( " File dialog to save a URL " ) ) ;
options . add ( " geticon [group] [context] " , ki18n ( " Icon chooser dialog " ) ) ;
options . add ( " progressbar <text> [totalsteps] " , ki18n ( " Progress bar dialog, returns a D-Bus reference for communication " ) ) ;
2008-01-14 20:20:16 +00:00
options . add ( " getcolor " , ki18n ( " Color dialog to select a color " ) ) ;
2007-07-02 08:49:20 +00:00
// TODO gauge stuff, reading values from stdin
options . add ( " title <text> " , ki18n ( " Dialog title " ) ) ;
2008-01-14 20:20:16 +00:00
options . add ( " default <text> " , ki18n ( " Default entry to use for combobox, menu and color " ) ) ;
2007-07-02 08:49:20 +00:00
options . add ( " multiple " , ki18n ( " Allows the --getopenurl and --getopenfilename options to return multiple files " ) ) ;
options . add ( " separate-output " , ki18n ( " Return list items on separate lines (for checklist option and file open with --multiple) " ) ) ;
options . add ( " print-winid " , ki18n ( " Outputs the winId of each dialog " ) ) ;
2008-03-04 13:28:10 +00:00
options . add ( " dontagain <file:entry> " , ki18n ( " Config file and option name for saving the \" do-not-show/ask-again \" state " ) ) ;
2008-08-31 12:42:25 +00:00
# ifdef Q_WS_X11
/* kdialog originally used --embed for attaching the dialog box. However this is misleading and so we changed to --attach.
* For backwards compatibility , we silently map - - embed to - - attach */
options . add ( " attach <winid> " , ki18n ( " Makes the dialog transient for an X app specified by winid " ) ) ;
options . add ( " embed <winid> " ) ;
# endif
2007-07-02 08:49:20 +00:00
options . add ( " +[arg] " , ki18n ( " Arguments - depending on main option " ) ) ;
2008-08-31 12:42:25 +00:00
2002-07-17 14:21:33 +00:00
KCmdLineArgs : : addCmdLineOptions ( options ) ; // Add our own options.
KApplication app ;
KCmdLineArgs * args = KCmdLineArgs : : parsedArgs ( ) ;
// execute direct kdialog command
return directCommand ( args ) ;
}
2003-08-31 01:12:27 +00:00