| /* qdialog |
| Copyright 2007-2014 PC GO Ld. |
| |
| 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 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| */ |
| |
| #include <stdio.h> |
| #include <string.h> |
| #include <QtCore/QLibraryInfo> |
| #include <QtCore/QLocale> |
| #include <QtCore/QTranslator> |
| #include <QtGui/QApplication> |
| #include <QtGui/QMessageBox> |
| #include <QtGui/QIcon> |
| #include <QtGui/QFileDialog> |
| #include <QtGui/QInputDialog> |
| #include <QtGui/QDialog> |
| #ifdef Q_OS_MAC |
| #include <QtGui/QStyle> |
| #endif |
| |
| #define VERSION "1.4" |
| |
| int GetFileName(QString title, QString filename, bool multiple) { |
| //char *out; |
| if(multiple) { |
| QStringList files = QFileDialog::getOpenFileNames(NULL, title, filename, QString(), NULL, QFileDialog::DontUseNativeDialog); |
| //QString line = NULL; |
| //printf("count = %d\nsize = %d\n", files.count(), files.size()); |
| int count = files.count(); |
| if(!count) return 0; |
| //int s = count, size = 0; |
| //while(s--) size += files[s].size() + 1; |
| //printf("size = %d\n", size); |
| //char buffer[size]; |
| //*buffer = 0; |
| //printf("*buffer = %hhd\n", *buffer); |
| for(int i=0; i<count; ++i) { |
| //if(i) line += "|"; |
| //line += files[i]; |
| if(i) putchar('|'); |
| printf("%s", files[i].toLocal8Bit().data()); |
| } |
| //out = QByteArray(line.toLocal8Bit()).data(); |
| //out = buffer; |
| putchar('\n'); |
| return 1; |
| } |
| char *out = QFileDialog::getOpenFileName(NULL, title, filename, QString(), NULL, QFileDialog::DontUseNativeDialog).toLocal8Bit().data(); |
| if(*out) { |
| puts(out); |
| return 1; |
| } |
| return 0; |
| } |
| |
| int GetText(QString title, QString text, QString etext) { |
| if(text.isEmpty()) text = QObject::trUtf8("Enter text:"); |
| bool ok; |
| char *out = QByteArray(QInputDialog::getText(0, title, text, QLineEdit::Normal, etext, &ok).toLocal8Bit()).data(); |
| if(ok) puts(out); |
| return ok; |
| } |
| |
| int PopMessageBox(QString title, QString text, int type, int button) { |
| QMessageBox box; |
| if(title.isEmpty()) switch(type) { |
| case QMessageBox::Question: |
| title = QApplication::translate("QDialog", "Question", 0, QApplication::UnicodeUTF8); |
| break; |
| case QMessageBox::Information: |
| title = QApplication::translate("QDialog", "Information", 0, QApplication::UnicodeUTF8); |
| break; |
| case QMessageBox::Warning: |
| title = QApplication::translate("QDialog", "Warning", 0, QApplication::UnicodeUTF8); |
| break; |
| case QMessageBox::Critical: |
| title = QApplication::translate("QDialog", "Error", 0, QApplication::UnicodeUTF8); |
| break; |
| } |
| box.setWindowTitle(title); |
| box.setText(text); |
| #ifdef Q_OS_MAC |
| if(QApplication::style()->objectName() != "macintosh (aqua)" || |
| type == QMessageBox::Warning || type == QMessageBox::Critical) |
| #endif |
| box.setIcon((QMessageBox::Icon)type); |
| if(button == 2) { |
| box.setStandardButtons(box.Ok | box.Cancel); |
| box.setDefaultButton(box.Ok); |
| } |
| return box.exec() == 1024; |
| } |
| |
| int main(int argc, char *argv[]) { |
| if(argc < 2) { |
| fprintf(stderr, "Missing argument, please see --help for usages.\n"); |
| return -1; |
| } |
| |
| if(argv[1] == QString("-h") || argv[1] == QString("--help")) { |
| fprintf(stderr, "qdialog - display Qt dialog from shell scripts.\nVersion: %s\nCopyright 2007-2014 PC GO Ld.\n\n" |
| "Usage: \n" |
| " %s [<general options...>] <message box type> [--double-button]" |
| #ifndef Q_OS_MAC |
| " [--title <title>]" |
| #endif |
| " --text <text>\n" |
| " %s [<general options...>] --entry [--title <title>] [--text <text>] [--entry-text <text>]\n" |
| " %s [<general options...>] --file-selection [--multiple] [--title <title>] [--filename <filename>]\n" |
| " %s [<general options...>] --aboutqt\n" |
| " %s -h|--help\n\n" |
| "General options:\n -l|--language <QLocale name> Set the language\n --style <QStyle name> Set the GUI style\n --window-icon <path> Set the window icon\n\n" |
| "The message box type are:\n --info Display information dialog\n --warning Display warning dialog\n --error Display error dialog\n --question Display question dialog\n\n", |
| VERSION, argv[0], argv[0], argv[0], argv[0], argv[0]); |
| return 0; |
| } |
| |
| for(int i=1; i<argc; i++) if(strcmp(argv[i], "--style") == 0) { |
| QApplication::setStyle(argv[i+1]); |
| for(int j=i; j<argc-1; j++) argv[j] = argv[j+2]; |
| argc -= 2; |
| } |
| |
| QApplication a(argc, argv); |
| for(int i=1; i<argc; i++) if(strcmp(argv[i], "--window-icon") == 0) { |
| a.setWindowIcon(QIcon(QString::fromUtf8(argv[i+1]))); |
| for(int j=i; j<argc-1; j++) argv[j] = argv[j+2]; |
| argc -= 2; |
| } |
| QString Locale = QLocale::system().name(); |
| for(int i=1; i<argc; i++) if(strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--language") == 0) { |
| Locale = argv[i+1]; |
| for(int j=i; j<argc-1; j++) argv[j] = argv[j+2]; |
| argc -= 2; |
| } |
| QTranslator translator; |
| translator.load("qt_" + Locale + ".qm", QLibraryInfo::location(QLibraryInfo::TranslationsPath)); |
| a.installTranslator(&translator); |
| if(argv[1] == QString("--aboutqt")) { |
| a.aboutQt(); |
| return -1; |
| } |
| |
| int type; |
| char *title = NULL; |
| char *text; |
| if(argv[1] == QString("--question")) type = QMessageBox::Question; |
| else if(argv[1] == QString("--info")) type = QMessageBox::Information; |
| else if(argv[1] == QString("--warning")) type = QMessageBox::Warning; |
| else if(argv[1] == QString("--error")) type = QMessageBox::Critical; |
| else if(argv[1] == QString("--entry")) type = 5; |
| else if(argv[1] == QString("--file-selection")) type = 7; |
| else { |
| fprintf(stderr, "Not a legal dialog type, please see --help for usages.\n"); |
| return 2; |
| } |
| if(type == 1 || type == 2 || type == 3 || type == 4) { |
| int button = 1; |
| for(int i=1; i<argc; i++) if(strcmp(argv[i], "--double-button") == 0) { |
| button = 2; |
| for(int j=i; j<argc; j++) argv[j] = argv[j+1]; |
| argc--; |
| } |
| |
| if(argc < 4) goto error; |
| if(argv[2] == QString("--title")) { |
| if(argc < 6) goto error; |
| title = argv[3]; |
| if(argv[4] == QString("--text")) text = argv[5]; |
| else goto error; |
| } else if(argv[2] == QString("--text")) { |
| text = argv[3]; |
| if(argv[4] == QString("--title")) title = argv[5]; |
| } else { |
| fprintf(stderr, "Unknown argument: %s\n", argv[2]); |
| return 2; |
| } |
| |
| return !PopMessageBox(QString::fromUtf8(title), QString::fromUtf8(text), type, button); |
| } |
| |
| if(type == 5) { |
| text = NULL; |
| char *etext = NULL; |
| if(argv[2] == QString("--title")) { |
| if(argc < 4) goto error; |
| title = argv[3]; |
| if(argv[4] == QString("--text")) { |
| text = argv[5]; |
| if(argv[6] == QString("--entry-text")) etext = argv[7]; |
| } else if(argv[4] == QString("--entry-text")) { |
| etext = argv[5]; |
| if(argv[6] == QString("--text")) text = argv[7]; |
| } |
| } else if(argv[2] == QString("--text")) { |
| if(argc < 4) goto error; |
| text = argv[3]; |
| if(argv[4] == QString("--title")) { |
| title = argv[5]; |
| if(argv[6] == QString("--entry-text")) etext = argv[7]; |
| } else if(argv[4] == QString("--entry-text")) { |
| etext = argv[5]; |
| if(argv[6] == QString("--title")) title = argv[7]; |
| } |
| } else if(argv[2] == QString("--entry-text")) { |
| if(argc < 4) goto error; |
| etext = argv[3]; |
| if(argv[4] == QString("--title")) { |
| title = argv[5]; |
| if(argv[6] == QString("--text")) text = argv[7]; |
| } else if(argv[4] == QString("--text")) { |
| text = argv[5]; |
| if(argv[6] == QString("--title")) title = argv[7]; |
| } |
| } |
| return !GetText(QString::fromUtf8(title), QString::fromUtf8(text), QString::fromUtf8(etext)); |
| } |
| |
| if(type == 7) { |
| bool multiple = false; |
| char *filename = NULL; |
| for(int i=1; i<argc; i++) if(strcmp(argv[i], "--multiple") == 0) { |
| multiple = true; |
| for(int j=i; j<argc; j++) argv[j] = argv[j+1]; |
| argc--; |
| } |
| |
| if(argv[2] == QString("--title")) { |
| if(argc < 4) goto error; |
| title = argv[3]; |
| if(argv[4] == QString("--filename")) filename = argv[5]; |
| } else if(argv[2] == QString("--filename")) { |
| if(argc < 4) goto error; |
| filename = argv[3]; |
| if(argv[4] == QString("--title")) title = argv[5]; |
| } |
| |
| return !GetFileName(QString::fromUtf8(title), QString::fromUtf8(filename), multiple); |
| } |
| |
| error: |
| fprintf(stderr, "Arguments Error\n"); |
| return 2; |
| } |