-added Log feature for QT .

-fixed Prefences and General Dialog
-disabled show function from main.cpp it will not allow to start minimized, it blocks minimize gui function.


git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@686 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
defnax 2008-08-15 17:49:57 +00:00
parent 8b9e64f9f6
commit cb891ff08a
29 changed files with 961 additions and 1063 deletions

View File

@ -23,15 +23,12 @@ DEPENDPATH += . \
gui\bwgraph \
gui\chat \
gui\connect \
gui\filehash \
gui\images \
gui\moreinfo \
gui\Preferences \
gui\common\
gui\Settings \
gui\toaster \
gui\help\browser \
gui\authdlg \
gui\elastic
INCLUDEPATH += . \
@ -80,9 +77,7 @@ HEADERS += rshare.h \
gui/LogoBar.h \
gui/xprogressbar.h \
lang/languagesupport.h \
util/process.h \
util/registry.h \
util/string.h \
util/stringutil.h \
util/win32.h \
util/RetroStyleLabel.h \
util/dllexport.h \
@ -99,6 +94,7 @@ HEADERS += rshare.h \
util/rsversion.h \
util/RsAction.h \
util/printpreview.h \
util/log.h \
gui/bwgraph/bwgraph.h \
gui/profile/ProfileView.h \
gui/profile/ProfileEdit.h \
@ -110,9 +106,7 @@ HEADERS += rshare.h \
gui/connect/AddFriendWizard.h \
gui/msgs/ChanMsgDialog.h \
gui/msgs/ChanCreateDialog.h \
gui/filehash/FileHashDialog.h \
gui/images/retroshare_win.rc.h \
gui/moreinfo/moreinfo.h \
gui/Preferences/configpage.h \
gui/Preferences/configpagestack.h \
gui/Preferences/CryptographyDialog.h \
@ -143,7 +137,6 @@ HEADERS += rshare.h \
gui/Settings/ServerPage.h \
gui/Settings/NetworkPage.h \
gui/statusbar/peerstatus.h \
gui/authdlg/AuthorizationDialog.h \
gui/advsearch/advancedsearchdialog.h \
gui/advsearch/expressionwidget.h \
gui/advsearch/guiexprelement.h \
@ -216,8 +209,6 @@ FORMS += gui/ChatDialog.ui \
gui/connect/AddFriendWizard.ui \
gui/msgs/ChanMsgDialog.ui \
gui/msgs/ChanCreateDialog.ui \
gui/filehash/FileHashDialog.ui \
gui/moreinfo/moreinfo.ui \
gui/Preferences/CryptographyDialog.ui \
gui/Preferences/DirectoriesDialog.ui \
gui/Preferences/AppearanceDialog.ui \
@ -234,7 +225,6 @@ FORMS += gui/ChatDialog.ui \
gui/Settings/DirectoriesPage.ui \
gui/Settings/ServerPage.ui \
gui/Settings/NetworkPage.ui \
gui/authdlg/AuthorizationDialog.ui \
gui/advsearch/advancedsearchdialog.ui \
gui/advsearch/expressionwidget.ui \
gui/channels/ChannelsDialog.ui \
@ -299,9 +289,7 @@ SOURCES += main.cpp \
gui/LogoBar.cpp \
gui/xprogressbar.cpp \
lang/languagesupport.cpp \
util/process.cpp \
util/registry.cpp \
util/string.cpp \
util/stringutil.cpp \
util/win32.cpp \
util/RetroStyleLabel.cpp \
util/WidgetBackgroundImage.cpp \
@ -313,6 +301,7 @@ SOURCES += main.cpp \
util/rsversion.cpp \
util/RsAction.cpp \
util/printpreview.cpp \
util/log.cpp \
gui/bwgraph/bwgraph.cpp \
gui/profile/ProfileView.cpp \
gui/profile/ProfileEdit.cpp \
@ -324,8 +313,6 @@ SOURCES += main.cpp \
gui/connect/AddFriendWizard.cpp \
gui/msgs/ChanMsgDialog.cpp \
gui/msgs/ChanCreateDialog.cpp \
gui/filehash/FileHashDialog.cpp \
gui/moreinfo/moreinfo.cpp \
gui/Preferences/configpagestack.cpp \
gui/Preferences/CryptographyDialog.cpp \
gui/Preferences/DirectoriesDialog.cpp \
@ -350,7 +337,6 @@ SOURCES += main.cpp \
gui/toaster/CallToaster.cpp \
gui/toaster/OnlineToaster.cpp \
gui/toaster/QtToaster.cpp \
gui/authdlg/AuthorizationDialog.cpp \
gui/advsearch/advancedsearchdialog.cpp \
gui/advsearch/expressionwidget.cpp \
gui/advsearch/guiexprelement.cpp \

View File

@ -107,6 +107,9 @@ MainWindow::MainWindow(QWidget* parent, Qt::WFlags flags)
/* Invoke the Qt Designer generated QObject setup routine */
ui.setupUi(this);
/* Create RshareSettings object */
_settings = new RshareSettings();
setWindowTitle(tr("RetroShare %1").arg(retroshareVersion()));
mSMPlayer = NULL;
@ -115,6 +118,10 @@ MainWindow::MainWindow(QWidget* parent, Qt::WFlags flags)
// Setting icons
this->setWindowIcon(QIcon(QString::fromUtf8(":/images/rstray3.png")));
/*if(!_settings->value(QString::fromUtf8("StartMinimized"), false).toBool()) {
show();
}*/
/* Create all the dialogs of which we only want one instance */
_bandwidthGraph = new BandwidthGraph();
@ -516,9 +523,7 @@ void MainWindow::createActions()
_helpAct = new QAction(QIcon(IMG_HELP), tr("Help"), this);
connect(_helpAct, SIGNAL(triggered()), this, SLOT(showHelpDialog()));
//connect(ui.btntoggletoolbox, SIGNAL(toggled(bool)), this, SLOT(showToolboxFrame(bool)));
}
/** If the user attempts to quit the app, a check-warning is issued. This warning can be

View File

@ -53,7 +53,7 @@
#include "ui_MainWindow.h"
#include "../config/rsharesettings.h"
#include "Preferences/rsharesettings.h"
class SMPlayer;
@ -147,12 +147,6 @@ private slots:
void showSettings();
void setStyle();
/** Called when console button is toggled */
//void showConsoleFrame(bool show);
/** Called when console button is toggled */
//void showToolboxFrame(bool show);
/** Called when user attempts to quit via quit button*/
void doQuit();
@ -186,6 +180,8 @@ private:
PreferencesWindow* _preferencesWindow;
/** A RetroShareSettings object used for saving/loading settings */
RshareSettings* _settings;
/** Creates a new action for a Main page. */
QAction* createPageAction(QIcon img, QString text, QActionGroup *group);

View File

@ -28,7 +28,6 @@
#include "NetworkDialog.h"
#include "NetworkView.h"
#include "connect/ConnectDialog.h"
#include "authdlg/AuthorizationDialog.h"
#include "rsiface/rsiface.h"
#include "rsiface/rspeers.h"
#include <sstream>
@ -138,16 +137,12 @@ void NetworkDialog::connecttreeWidgetCostumPopupMenu( QPoint point )
peerdetailsAct = new QAction(QIcon(IMAGE_PEERDETAILS), tr( "Make Friend / Peer Details" ), this );
connect( peerdetailsAct , SIGNAL( triggered() ), this, SLOT( peerdetails() ) );
//authAct = new QAction(QIcon(IMAGE_AUTH), tr( "Authenticate" ), this );
//connect( authAct , SIGNAL( triggered() ), this, SLOT( peerdetails() ) );
loadcertAct = new QAction(QIcon(IMAGE_LOADCERT), tr( "Load Certificate" ), this );
connect( loadcertAct , SIGNAL( triggered() ), this, SLOT( loadneighbour() ) );
contextMnu.clear();
contextMnu.addAction( peerdetailsAct);
//contextMnu.addAction( authAct);
contextMnu.addAction( loadcertAct);
contextMnu.exec( mevent->globalPos() );
}
@ -182,23 +177,6 @@ void NetworkDialog::showpeerdetails(std::string id)
}
}
/** Shows Connect Dialog */
void NetworkDialog::showAuthDialog()
{
static AuthorizationDialog *authorizationdialog = new AuthorizationDialog();
QTreeWidgetItem *wi = getCurrentNeighbour();
if (!wi)
return;
RsCertId id = getNeighRsCertId(wi);
std::ostringstream out;
out << id;
authorizationdialog->setAuthCode(out.str(), wi->text(9).toStdString());
authorizationdialog->show();
}
/** Open a QFileDialog to browse for a pem/pqi file. */
void NetworkDialog::loadcert()

View File

@ -52,8 +52,6 @@ public slots:
private slots:
void showAuthDialog();
void peerdetails();
/** Create the context popup menu and it's submenus */

View File

@ -43,7 +43,7 @@ GeneralDialog::GeneralDialog(QWidget *parent)
} else {
/* Don't let people hide the main window, since that's all they have. */
ui.chkShowOnStartup->hide();
//show();
show();
}
/* Hide platform specific features */
#ifndef Q_WS_WIN
@ -64,6 +64,9 @@ GeneralDialog::save(QString &errmsg)
Q_UNUSED(errmsg);
_settings->setValue(QString::fromUtf8("StartMinimized"), startMinimized());
_settings->setRunRetroshareOnBoot(
ui.chkRunRetroshareAtSystemStartup->isChecked());
return true;
}

View File

@ -53,7 +53,7 @@ private slots:
void toggleShowOnStartup(bool checked);
private:
/** A VidaliaSettings object used for saving/loading settings */
/** A RetroShare Settings object used for saving/loading settings */
RshareSettings* _settings;

View File

@ -38,7 +38,9 @@
#define IMAGE_LOG ":/images/log_24x24.png"
#define IMAGE_ABOUT ":/images/informations_24x24.png"
#define IMAGE_SAVE ":/images/media-floppy.png"
#define IMAGE_HELP ":/images/help24.png"
#define IMAGE_HELP ":/images/help24.png"
#define IMAGE_APPEARRANCE ":/images/looknfeel.png"
/** Constructor */
PreferencesWindow::PreferencesWindow(QWidget *parent, Qt::WFlags flags)
@ -59,7 +61,7 @@ PreferencesWindow::PreferencesWindow(QWidget *parent, Qt::WFlags flags)
createPageAction(QIcon(IMAGE_DIRECTORIES), tr("Directories"), grp));
ui.stackPages->add(new AppearanceDialog(ui.stackPages),
createPageAction(QIcon(IMAGE_DIRECTORIES), tr("AppearanceDialog"), grp));
createPageAction(QIcon(IMAGE_APPEARRANCE), tr("Appearance"), grp));
/*foreach (ConfigPage *page, ui.stackPages->pages()) {
connect(page, SIGNAL(helpRequested(QString)),

View File

@ -22,7 +22,6 @@
#include "rshare.h"
#include "TransfersDialog.h"
#include "moreinfo/moreinfo.h"
#include "DLListDelegate.h"
#include "ULListDelegate.h"
@ -186,8 +185,6 @@ void TransfersDialog::downloadListCostumPopupMenu( QPoint point )
contextMnu.addSeparator();
contextMnu.addAction( cancelAct);
// contextMnu.addSeparator();
// contextMnu.addAction( showdowninfoAct);
contextMnu.addSeparator();
contextMnu.addAction( clearcompletedAct);
contextMnu.exec( mevent->globalPos() );
@ -224,13 +221,6 @@ void TransfersDialog::playSelectedTransfer()
}
/** Shows Downloads Informations */
void TransfersDialog::showDownInfoWindow()
{
moreinfo *detailsdlg = new moreinfo();
detailsdlg->show();
}
void TransfersDialog::updateProgress(int value)
{
for(int i = 0; i <= DLListModel->rowCount(); i++) {

View File

@ -53,7 +53,6 @@ public:
void insertTransfers();
private slots:
void showDownInfoWindow();
/** Create the context popup menu and it's submenus */
void downloadListCostumPopupMenu( QPoint point );

View File

@ -1,123 +0,0 @@
/****************************************************************
* RetroShare is distributed under the following license:
*
* Copyright (C) 2006, crypton
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#include "rshare.h"
#include "AuthorizationDialog.h"
#include "rsiface/rsiface.h"
#include "rsiface/rspeers.h"
#include <iostream>
/** Default constructor */
AuthorizationDialog::AuthorizationDialog(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
/* Invoke Qt Designer generated QObject setup routine */
ui.setupUi(this);
// GConfig config;
// config.loadWidgetInformation(this);
// Create the status bar
//statusBar()->showMessage("Please enter the correct AUTH CODE !");
setFixedSize(QSize(267, 103));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(closedlg()));
connect(ui.okButton, SIGNAL(clicked()), this, SLOT(authAttempt()));
//connect(ui.Ledit_name, SIGNAL(textChanged()), this, SLOT(checkAuthCode()));
connect(ui.Ledit_name, SIGNAL(textChanged ( const QString & ) ), this, SLOT(checkAuthCode( const QString & )));
}
/**
Overloads the default show() slot so we can set opacity*/
void
AuthorizationDialog::show()
{
//loadSettings();
if(!this->isVisible()) {
QDialog::show();
}
}
void AuthorizationDialog::closeEvent (QCloseEvent * event)
{
// GConfig config;
// config.saveWidgetInformation(this);
QWidget::closeEvent(event);
}
void AuthorizationDialog::closedlg()
{
close();
}
void AuthorizationDialog::setAuthCode(std::string id, std::string code)
{
authId = id;
authCode = code;
ui.Ledit_name->setText(QString::fromStdString(code));
//ui.okButton ->setEnabled(true);
}
void AuthorizationDialog::checkAuthCode(const QString &txt)
{
//std::cerr << "AuthCode:" << authCode << std::endl;
//std::cerr << "Entered:" << ui.Ledit_name -> text().toStdString() << std::endl;
//std::cerr << "Entered:" << txt.toStdString() << std::endl;
if (authCode == txt.toStdString())
{
/* enable ok button */
ui.okButton ->setEnabled(true);
}
else
{
/* disable ok button */
ui.okButton ->setEnabled(false);
}
}
void AuthorizationDialog::authAttempt()
{
/* well lets do it ! */
std::cerr << "Attempting AuthCode:" << authCode << std::endl;
rsPeers->AuthCertificate(authId, authCode);
rsPeers->addFriend(authId);
/* close it up! */
closedlg();
}

View File

@ -1,70 +0,0 @@
/****************************************************************
* RShare is distributed under the following license:
*
* Copyright (C) 2006, crypton
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#ifndef _AUTHORIZATIONDIALOG_H
#define _AUTHORIZATIONDIALOG_H
#include <config/rsharesettings.h>
#include "ui_AuthorizationDialog.h"
#include <string>
class AuthorizationDialog : public QDialog
{
Q_OBJECT
public:
/** Default constructor */
AuthorizationDialog(QWidget *parent = 0, Qt::WFlags flags = 0);
/** Default destructor */
void setAuthCode(std::string id, std::string code);
public slots:
/** Overloaded QWidget.show */
void checkAuthCode( const QString &txt );
void show();
protected:
void closeEvent (QCloseEvent * event);
private slots:
void closedlg();
void authAttempt();
private:
std::string authCode;
std::string authId;
/** Loads the saved connectidialog settings */
// void loadSettings();
/** Qt Designer generated object */
Ui::AuthorizationDialog ui;
};
#endif

View File

@ -1,148 +0,0 @@
<ui version="4.0" >
<class>AuthorizationDialog</class>
<widget class="QDialog" name="AuthorizationDialog" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>267</width>
<height>103</height>
</rect>
</property>
<property name="windowTitle" >
<string>Authenticate</string>
</property>
<property name="windowIcon" >
<iconset resource="../images.qrc" >:/images/encrypted22.png</iconset>
</property>
<layout class="QVBoxLayout" >
<property name="margin" >
<number>9</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item>
<widget class="QLabel" name="label" >
<property name="sizePolicy" >
<sizepolicy>
<hsizetype>5</hsizetype>
<vsizetype>5</vsizetype>
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text" >
<string>Authenticate Friend By Entering Their Code</string>
</property>
</widget>
</item>
<item>
<layout class="QGridLayout" >
<property name="margin" >
<number>0</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item row="0" column="0" >
<widget class="QLabel" name="label_2" >
<property name="text" >
<string>AUTH CODE:</string>
</property>
</widget>
</item>
<item row="0" column="1" >
<widget class="QLineEdit" name="Ledit_name" >
<property name="sizePolicy" >
<sizepolicy>
<hsizetype>0</hsizetype>
<vsizetype>0</vsizetype>
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="2" >
<spacer>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType" >
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" >
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" >
<property name="margin" >
<number>0</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item>
<spacer>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" >
<size>
<width>131</width>
<height>31</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="okButton" >
<property name="enabled" >
<bool>false</bool>
</property>
<property name="minimumSize" >
<size>
<width>77</width>
<height>0</height>
</size>
</property>
<property name="text" >
<string>OK</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cancelButton" >
<property name="minimumSize" >
<size>
<width>77</width>
<height>0</height>
</size>
</property>
<property name="text" >
<string>Cancel</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<tabstops>
<tabstop>Ledit_name</tabstop>
<tabstop>okButton</tabstop>
<tabstop>cancelButton</tabstop>
</tabstops>
<resources>
<include location="../images.qrc" />
</resources>
<connections/>
</ui>

View File

@ -1,57 +0,0 @@
/****************************************************************
* RetroShare is distributed under the following license:
*
* Copyright (C) 2006, crypton
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#include "moreinfo.h"
/** Default constructor */
moreinfo::moreinfo(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
/* Invoke Qt Designer generated QObject setup routine */
ui.setupUi(this);
setFixedSize(QSize(400, 300));
//Closing window
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(cancel()));
}
void
moreinfo::show()
{
//loadSettings();
if(!this->isVisible()) {
QDialog::show();
}
}
void moreinfo::closeEvent (QCloseEvent * event)
{
QWidget::closeEvent(event);
}
void moreinfo::cancel()
{
close();
}

View File

@ -1,58 +0,0 @@
/****************************************************************
* RetroShare is distributed under the following license:
*
* Copyright (C) 2006, crypton
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#ifndef _MOREINFO_H
#define _MOREINFO_H
#include "ui_moreinfo.h"
class moreinfo : public QDialog
{
Q_OBJECT
public:
/** Default constructor */
moreinfo(QWidget *parent = 0, Qt::WFlags flags = 0);
/** Default destructor */
public slots:
/** Overloaded QWidget.show */
void show();
void cancel();
protected:
void closeEvent (QCloseEvent * event);
private slots:
private:
/** Qt Designer generated object */
Ui::moreinfo ui;
};
#endif

View File

@ -1,51 +0,0 @@
<ui version="4.0" >
<class>moreinfo</class>
<widget class="QDialog" name="moreinfo" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle" >
<string>Details</string>
</property>
<widget class="QPushButton" name="pushButton" >
<property name="geometry" >
<rect>
<x>310</x>
<y>270</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text" >
<string>OK</string>
</property>
</widget>
<widget class="QTableWidget" name="tableWidget" >
<property name="geometry" >
<rect>
<x>10</x>
<y>10</y>
<width>381</width>
<height>251</height>
</rect>
</property>
<column>
<property name="text" >
<string>Name</string>
</property>
</column>
<column>
<property name="text" >
<string>Value</string>
</property>
</column>
</widget>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -25,6 +25,7 @@
#include <gui/MainWindow.h>
#include <gui/StartDialog.h>
#include <gui/GenCertDialog.h>
#include <Gui/Preferences/Rsharesettings.h>
/*** WINDOWS DON'T LIKE THIS - REDEFINES VER numbers.
#include <gui/qskinobject/qskinobject.h>
@ -144,22 +145,24 @@ int main(int argc, char *argv[])
notify->setChannelsDialog(w->channelsDialog);
notify->setMessengerWindow(w->messengerWindow);
/* only show window, if not startMinimised */
if (!startMinimised)
/* only show window, if not startMinimized */
/*if (!startMinimised)
{
w->show();
//skinWindow->show();
}
}*/
/* Run Retroshare */
int ret = rshare.run();
/* Startup a Timer to keep the gui's updated */
QTimer *timer = new QTimer(w);
timer -> connect(timer, SIGNAL(timeout()), notify, SLOT(UpdateGUI()));
timer->start(1000);
/* dive into the endless loop */
return rshare.exec();
/* dive into the endless loop */
return ret;
}

View File

@ -23,9 +23,14 @@
#include <QDir>
#include <QTimer>
#include <QTextStream>
#include <QStyleFactory>
#include <util/string.h>
#include <gui/common/vmessagebox.h>
#include <gui/common/html.h>
#include <util/stringutil.h>
#include <stdlib.h>
#include <lang/languagesupport.h>
#include "gui/Preferences/rsharesettings.h"
@ -34,20 +39,48 @@
/* Available command-line arguments. */
#define ARG_LANGUAGE "lang" /**< Argument specifying language. */
#define ARG_GUISTYLE "style" /**< Argument specfying GUI style. */
#define ARG_GUISTYLESHEET "stylesheet" /**< Argument specfying GUI style. */
#define ARG_GUISTYLESHEET "stylesheet" /**< Argument specfying GUI style. */
#define ARG_RESET "reset" /**< Reset Rshare's saved settings. */
#define ARG_DATADIR "datadir" /**< Directory to use for data files. */
#define ARG_LOGFILE "logfile" /**< Location of our logfile. */
#define ARG_LOGLEVEL "loglevel" /**< Log verbosity. */
/* Static member variables */
QMap<QString, QString> Rshare::_args; /**< List of command-line arguments. */
QString Rshare::_style; /**< The current GUI style. */
QString Rshare::_language; /**< The current language. */
QString Rshare::_stylesheet; /**< The current GUI style. */
QString Rshare::_stylesheet; /**< The current GUI stylesheet. */
Log Rshare::_log;
bool Rshare::useConfigDir;
QString Rshare::configDir;
/** Catches debugging messages from Qt and sends them to RetroShare's logs. If Qt
* emits a QtFatalMsg, we will write the message to the log and then abort().
*/
void
Rshare::qt_msg_handler(QtMsgType type, const char *s)
{
QString msg(s);
switch (type) {
case QtDebugMsg:
rDebug("QtDebugMsg: %1").arg(msg);
break;
case QtWarningMsg:
rNotice("QtWarningMsg: %1").arg(msg);
break;
case QtCriticalMsg:
rWarn("QtCriticalMsg: %1").arg(msg);
break;
case QtFatalMsg:
rError("QtFatalMsg: %1").arg(msg);
break;
}
if (type == QtFatalMsg) {
rError("Fatal Qt error. Aborting.");
abort();
}
}
/** Constructor. Parses the command-line arguments, resets Rshare's
* configuration (if requested), and sets up the GUI style and language
@ -55,6 +88,8 @@ QString Rshare::configDir;
Rshare::Rshare(QStringList args, int &argc, char **argv, QString dir)
: QApplication(argc, argv)
{
qInstallMsgHandler(qt_msg_handler);
/* Read in all our command-line arguments. */
parseArguments(args);
@ -63,6 +98,19 @@ Rshare::Rshare(QStringList args, int &argc, char **argv, QString dir)
RshareSettings settings;
settings.reset();
}
/* Handle the -loglevel and -logfile options. */
if (_args.contains(ARG_LOGFILE))
_log.open(_args.value(ARG_LOGFILE));
if (_args.contains(ARG_LOGLEVEL)) {
_log.setLogLevel(Log::stringToLogLevel(
_args.value(ARG_LOGLEVEL)));
if (!_args.contains(ARG_LOGFILE))
_log.open(stdout);
}
if (!_args.contains(ARG_LOGLEVEL) &&
!_args.contains(ARG_LOGFILE))
_log.setLogLevel(Log::Off);
/* config directory */
useConfigDir = false;
@ -93,6 +141,24 @@ Rshare::~Rshare()
}
/** Enters the main event loop and waits until exit() is called. The signal
* running() will be emitted when the event loop has started. */
int
Rshare::run()
{
QTimer::singleShot(0, rApp, SLOT(onEventLoopStarted()));
return rApp->exec();
}
/** Called when the application's main event loop has started. This method
* will emit the running() signal to indicate that the application's event
* loop is running. */
void
Rshare::onEventLoopStarted()
{
emit running();
}
#if defined(Q_OS_WIN)
/** On Windows, we need to catch the WM_QUERYENDSESSION message
* so we know that it is time to shutdown. */
@ -107,29 +173,63 @@ Rshare::winEventFilter(MSG *msg, long *result)
#endif
/** Display usage information regarding command-line arguments. */
void
/*void
Rshare::printUsage(QString errmsg)
{
QTextStream out(stdout);
QTextStream out(stdout);*/
/* If there was an error message, print it out. */
if (!errmsg.isEmpty()) {
/*if (!errmsg.isEmpty()) {
out << "** " << errmsg << " **" << endl << endl;
}
}*/
/* Now print the application usage */
out << "Usage: " << endl;
out << "\t" << qApp->arguments().at(0) << " [options]" << endl;
//out << "Usage: " << endl;
//out << "\t" << qApp->arguments().at(0) << " [options]" << endl;
/* And available options */
out << endl << "Available Options:" << endl;
out << "\t-"ARG_RESET"\t\tResets ALL stored Rshare settings." << endl;
out << "\t-"ARG_DATADIR"\tSets the directory Rshare uses for data files"<< endl;
out << "\t-"ARG_GUISTYLE"\t\tSets Rshare's interface style." << endl;
out << "\t-"ARG_GUISTYLESHEET"\t\tSets Rshare's stylesheet." << endl;
out << "\t\t\t[" << QStyleFactory::keys().join("|") << "]" << endl;
out << "\t-"ARG_LANGUAGE"\t\tSets Rshare's language." << endl;
out << "\t\t\t[" << LanguageSupport::languageCodes().join("|") << "]" << endl;
//out << endl << "Available Options:" << endl;
//out << "\t-"ARG_RESET"\t\tResets ALL stored Rshare settings." << endl;
//out << "\t-"ARG_DATADIR"\tSets the directory Rshare uses for data files"<< endl;
//out << "\t-"ARG_GUISTYLE"\t\tSets Rshare's interface style." << endl;
//out << "\t-"ARG_GUISTYLESHEET"\t\tSets Rshare's stylesheet." << endl;
//out << "\t\t\t[" << QStyleFactory::keys().join("|") << "]" << endl;
//out << "\t-"ARG_LANGUAGE"\t\tSets Rshare's language." << endl;
//out << "\t\t\t[" << LanguageSupport::languageCodes().join("|") << "]" << endl;
//}
/** Displays usage information for command-line args. */
void
Rshare::showUsageMessageBox()
{
QString usage;
QTextStream out(&usage);
out << "Available Options:" << endl;
out << "<table>";
//out << trow(tcol("-"ARG_HELP) +
// tcol(tr("Displays this usage message and exits.")));
out << trow(tcol("-"ARG_RESET) +
tcol(tr("Resets ALL stored RetroShare settings.")));
out << trow(tcol("-"ARG_DATADIR" &lt;dir&gt;") +
tcol(tr("Sets the directory RetroShare uses for data files.")));
out << trow(tcol("-"ARG_LOGFILE" &lt;file&gt;") +
tcol(tr("Sets the name and location of RetroShare's logfile.")));
out << trow(tcol("-"ARG_LOGLEVEL" &lt;level&gt;") +
tcol(tr("Sets the verbosity of Vidalia's logging.") +
"<br>[" + Log::logLevels().join("|") +"]"));
out << trow(tcol("-"ARG_GUISTYLE" &lt;style&gt;") +
tcol(tr("Sets RetroShare's interface style.") +
"<br>[" + QStyleFactory::keys().join("|") + "]"));
out << trow(tcol("-"ARG_GUISTYLESHEET" &lt;stylesheet&gt;") +
tcol(tr("Sets RetroShare's interface stylesheets.")));
out << trow(tcol("-"ARG_LANGUAGE" &lt;language&gt;") +
tcol(tr("Sets RetroShare's language.") +
"<br>[" + LanguageSupport::languageCodes().join("|") + "]"));
out << "</table>";
VMessageBox::information(0,
tr("RetroShare Usage Information"), usage, VMessageBox::Ok);
}
/** Returns true if the specified argument expects a value. */
@ -139,7 +239,9 @@ Rshare::argNeedsValue(QString argName)
return (argName == ARG_GUISTYLE ||
argName == ARG_GUISTYLESHEET ||
argName == ARG_LANGUAGE ||
argName == ARG_DATADIR);
argName == ARG_DATADIR ||
argName == ARG_LOGFILE ||
argName == ARG_LOGLEVEL);
}
@ -173,10 +275,6 @@ Rshare::parseArguments(QStringList args)
bool
Rshare::validateArguments(QString &errmsg)
{
/* If they want help, just return false now
if (_args.contains(ARG_HELP)) {
return false;
}*/
/* Check for a language that Retroshare recognizes. */
if (_args.contains(ARG_LANGUAGE) &&
!LanguageSupport::isValidLanguageCode(_args.value(ARG_LANGUAGE))) {
@ -190,6 +288,19 @@ Rshare::validateArguments(QString &errmsg)
errmsg = tr("Invalid GUI style specified: ") + _args.value(ARG_GUISTYLE);
return false;
}
/* Check for a valid log level */
if (_args.contains(ARG_LOGLEVEL) &&
!Log::logLevels().contains(_args.value(ARG_LOGLEVEL))) {
errmsg = tr("Invalid log level specified: ") + _args.value(ARG_LOGLEVEL);
return false;
}
/* Check for a writable log file */
if (_args.contains(ARG_LOGFILE) && !_log.isOpen()) {
errmsg = tr("Unable to open log file '%1': %2")
.arg(_args.value(ARG_LOGFILE))
.arg(_log.errorString());
return false;
}
return true;
}
@ -242,23 +353,11 @@ Rshare::setSheet(QString sheet)
sheet = settings.getSheetName();
}
/* Apply the specified GUI stylesheet */
/*if (QApplication::setSheet(sheet)) {*/
_stylesheet = sheet;
return true;
/*}
return false;*/
}
/** Displays the help page associated with the specified topic. If no topic is
* specified, then the default help page will be displayed. */
/**void
Rshare::help(QString topic)
{
_help->show(topic);
}*/
/** Returns the directory RetroShare uses for its data files. */
QString
Rshare::dataDirectory()
@ -299,7 +398,6 @@ Rshare::createDataDirectory(QString *errmsg)
return true;
}
/** Set Rshare's data directory - externally */
bool Rshare::setConfigDirectory(QString dir)
{
@ -308,5 +406,11 @@ bool Rshare::setConfigDirectory(QString dir)
return true;
}
/** Writes <b>msg</b> with severity <b>level</b> to Vidalia's log. */
Log::LogMessage
Rshare::log(Log::LogLevel level, QString msg)
{
return _log.log(level, msg);
}

View File

@ -34,12 +34,21 @@
#include <QMap>
#include <QString>
#include "util/log.h"
#include "gui/Preferences/rsharesettings.h"
/** Rshare's version string */
#define RSHARE_VERSION "0.7"
/** Pointer to this RetroShare application instance. */
#define rApp ((Rshare *)qApp)
#define rDebug(fmt) (rApp->log(Log::Debug, (fmt)))
#define rInfo(fmt) (rApp->log(Log::Info, (fmt)))
#define rNotice(fmt) (rApp->log(Log::Notice, (fmt)))
#define rWarn(fmt) (rApp->log(Log::Warn, (fmt)))
#define rError(fmt) (rApp->log(Log::Error, (fmt)))
class Rshare : public QApplication
{
@ -56,7 +65,11 @@ public:
/** Validates that all arguments were well-formed. */
bool validateArguments(QString &errmsg);
/** Prints usage information to the given text stream. */
void printUsage(QString errmsg = QString());
//void printUsage(QString errmsg = QString());
/** Displays usage information for command-line args. */
static void showUsageMessageBox();
/** Returns true if the user wants to see usage information. */
static bool showUsage();
/** Sets the current language. */
static bool setLanguage(QString languageCode = QString());
@ -84,10 +97,20 @@ public:
/** Creates Rshare's data directory, if it doesn't already exist. */
static bool createDataDirectory(QString *errmsg);
/** Writes <b>msg</b> with severity <b>level</b> to RetroShare's log. */
static Log::LogMessage log(Log::LogLevel level, QString msg);
/** Creates Rshare's data directory, if it doesn't already exist. */
static bool setConfigDirectory(QString dir);
/** Enters the main event loop and waits until exit() is called. The signal
* running() will be emitted when the event loop has started. */
static int run();
signals:
/** Emitted when the application is running and the main event loop has
* started. */
void running();
/** Signals that the application needs to shutdown now. */
void shutdown();
@ -97,7 +120,18 @@ protected:
bool winEventFilter(MSG *msg, long *result);
#endif
private slots:
/** Called when the application's main event loop has started. This method
* will emit the running() signal to indicate that the application's event
* loop is running. */
void onEventLoopStarted();
private:
/** Catches debugging messages from Qt and sends them to
* RetroShare's logs. */
static void qt_msg_handler(QtMsgType type, const char *msg);
/** Parse the list of command-line arguments. */
void parseArguments(QStringList args);
/** Returns true if the specified arguments wants a value. */
@ -105,8 +139,9 @@ private:
static QMap<QString, QString> _args; /**< List of command-line arguments. */
static QString _style; /**< The current GUI style. */
static QString _stylesheet; /**< The current GUI stylesheet. */
static QString _stylesheet; /**< The current GUI stylesheet. */
static QString _language; /**< The current language. */
static Log _log; /**< Logs debugging messages to file or stdout. */
static bool useConfigDir;
static QString configDir;

View File

@ -0,0 +1,181 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2008, crypton
* Copyright (c) 2008, Matt Edman, Justin Hipple
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#include <QDateTime>
#include <QTextStream>
#include "log.h"
/** Open log files for appending as write-only text. */
#define LOGFILE_MODE \
(QIODevice::WriteOnly|QIODevice::Append|QIODevice::Text)
/** Format for log message timestamps. */
#define TIMESTAMP_FMT "MMM dd HH:mm:ss.zzz"
/** Default constructor. Logs at level Notice by default. */
Log::Log()
{
_logLevel = Notice;
}
/** Destructor. Closes the log file. */
Log::~Log()
{
close();
}
/** Returns a list of strings representing available log levels. */
QStringList
Log::logLevels()
{
return (QStringList() << "debug" << "info" << "notice"
<< "warn" << "error");
}
/** Sets the current log level to <b>level</b>. If <b>level</b> is Off, then
* the log file will be closed as well. If <b>level</b> is Unknown, no change
* to the current log level is made. */
void
Log::setLogLevel(LogLevel level)
{
if (level >= Debug && level < Unknown)
_logLevel = level;
if (level == Off)
_logFile.close();
}
/** Opens <b>file</b> for appending, to which log messages will be written. */
bool
Log::open(FILE *file)
{
if (_logFile.isOpen())
close();
_logFile.open(file, LOGFILE_MODE);
return isOpen();
}
/** Opens <b>file</b> for appending, to which log messages will be written. */
bool
Log::open(QString file)
{
if (_logFile.isOpen())
close();
_logFile.setFileName(file);
_logFile.open(LOGFILE_MODE);
return isOpen();
}
/** Flushes any outstanding log messages and closes the log file. */
void
Log::close()
{
if (_logFile.isOpen()) {
_logFile.flush();
_logFile.close();
}
}
/** Creates a log message with severity <b>level</b> and initial message
* contents <b>message</b>. The log message can be appended to until the
* returned LogMessage's destructor is called, at which point the complete
* message is written to the log file. */
inline Log::LogMessage
Log::log(LogLevel level)
{
if (level < _logLevel)
return LogMessage(level, 0);
return LogMessage(level, &_logFile);
}
/** Creates a log message with severity <b>level</b>. The log message can be
* appended to until the returned LogMessage's destructor is called, at
* which point the complete message is written to the log file. */
Log::LogMessage
Log::log(LogLevel level, QString msg)
{
return log(level) << msg;
}
/** Returns a string description of the given LogLevel <b>level</b>. */
inline QString
Log::logLevelToString(LogLevel level)
{
switch (level) {
case Debug: return "debug";
case Info: return "info";
case Notice: return "notice";
case Warn: return "warn";
case Error: return "error";
case Off: return "off";
default: return "unknown";
}
}
/** Returns a LogLevel for the level given by <b>str</b>, or Unknown if the
* given string does not represent a valid LogLevel value. */
Log::LogLevel
Log::stringToLogLevel(QString str)
{
str = str.toLower();
if (str == "debug")
return Debug;
else if (str == "info")
return Info;
else if (str == "notice")
return Notice;
else if (str == "warn")
return Warn;
else if (str == "error")
return Error;
else if (str == "off")
return Off;
return Unknown;
}
/** Returns a formatted log message, prefixed with a timestamp and the log
* message severity level. */
inline QString
Log::LogMessage::toString() const
{
QString msg = QDateTime::currentDateTime().toString(TIMESTAMP_FMT);
msg.append(" [" + Log::logLevelToString(stream->type) + "] ");
msg.append(stream->buf);
return msg;
}
/** Destructor. Writes the buffered log message out to the log file specified
* in the constructor. */
Log::LogMessage::~LogMessage()
{
if (!--stream->ref) {
if (stream->out && !stream->buf.isEmpty()) {
QTextStream log(stream->out);
log << toString() << endl;
log.flush();
}
delete stream;
}
}

View File

@ -0,0 +1,163 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2006-2007, crypton
* Copyright (c) 2006, Matt Edman, Justin Hipple
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#ifndef _LOG_H
#define _LOG_H
#include <QObject>
#include <QFile>
#include <QStringList>
#include <QIODevice>
#include <QHostAddress>
/** The Log class is similar to the QDebug class provided with Qt, but with
* finer-grained logging levels, slightly different output (for example, not
* everything is wrapped in double quotes), supports using .arg(), and can
* still be used even if Qt was compiled with QT_NO_DEBUG_STREAM. */
class Log
{
public:
/** Logging severity levels. */
enum LogLevel {
Debug = 0, /**< Verbose debugging output. */
Info, /**< Primarily program flow output. */
Notice, /**< Non-failure (but important) events. */
Warn, /**< Recoverable failure conditions. */
Error, /**< Critical, non-recoverable errors. */
Off, /**< No logging output. */
Unknown /**< Unknown/invalid log level. */
};
class LogMessage;
/** Default constructor. */
Log();
/** Destructor. */
~Log();
/** Opens a file on disk (or stdout or stderr) to which log messages will be
* written. */
bool open(FILE *file);
/** Opens a file on disk to which log messages will be written. */
bool open(QString file);
/** Closes the log file. */
void close();
/** Returns true if the log file is open and ready for writing. */
bool isOpen() { return _logFile.isOpen() && _logFile.isWritable(); }
/** Returns a string description of the last file error encountered. */
QString errorString() { return _logFile.errorString(); }
/** Sets the current log level to <b>level</b>. */
void setLogLevel(LogLevel level);
/** Returns a list of strings representing valid log levels. */
static QStringList logLevels();
/** Returns a string description of the given LogLevel <b>level</b>. */
static inline QString logLevelToString(LogLevel level);
/** Returns a LogLevel for the level given by <b>str</b>. */
static LogLevel stringToLogLevel(QString str);
/** Creates a log message with severity <b>level</b> and initial message
* contents <b>message</b>. The log message can be appended to until the
* returned LogMessage's destructor is called, at which point the complete
* message is written to the log file. */
LogMessage log(LogLevel level, QString message);
/** Creates a log message with severity <b>level</b>. The log message can be
* appended to until the returned LogMessage's destructor is called, at
* which point the complete message is written to the log file. */
inline LogMessage log(LogLevel level);
private:
LogLevel _logLevel; /**< Minimum log severity level. */
QFile _logFile; /**< Log output destination. */
};
/** This internal class represents a single message that is to be written to
* the log destination. The message is buffered until it is written to the
* log in this class's destructor. */
class Log::LogMessage
{
public:
struct Stream {
Stream(Log::LogLevel t, QIODevice *o)
: type(t), out(o), ref(1) {}
Log::LogLevel type;
QIODevice *out;
int ref;
QString buf;
} *stream;
inline LogMessage(Log::LogLevel t, QIODevice *o)
: stream(new Stream(t,o)) {}
inline LogMessage(const LogMessage &o)
: stream(o.stream) { ++stream->ref; }
inline QString toString() const;
~LogMessage();
/* Support both the << and .arg() methods */
inline LogMessage &operator<<(const QString &t)
{ stream->buf += t; return *this; }
inline LogMessage arg(const QString &a)
{ stream->buf = stream->buf.arg(a); return *this; }
inline LogMessage &operator<<(const QStringList &a)
{ stream->buf += a.join(","); return *this; }
inline LogMessage arg(const QStringList &a)
{ stream->buf = stream->buf.arg(a.join(",")); return *this; }
inline LogMessage &operator<<(const QHostAddress &a)
{ stream->buf += a.toString(); return *this; }
inline LogMessage arg(const QHostAddress &a)
{ stream->buf = stream->buf.arg(a.toString()); return *this; }
inline LogMessage &operator<<(short a)
{ stream->buf += QString::number(a); return *this; }
inline LogMessage arg(short a)
{ stream->buf = stream->buf.arg(a); return *this; }
inline LogMessage &operator<<(ushort a)
{ stream->buf += QString::number(a); return *this; }
inline LogMessage arg(ushort a)
{ stream->buf = stream->buf.arg(a); return *this; }
inline LogMessage &operator<<(int a)
{ stream->buf += QString::number(a); return *this; }
inline LogMessage arg(int a)
{ stream->buf = stream->buf.arg(a); return *this; }
inline LogMessage &operator<<(uint a)
{ stream->buf += QString::number(a); return *this; }
inline LogMessage arg(uint a)
{ stream->buf = stream->buf.arg(a); return *this; }
inline LogMessage &operator<<(long a)
{ stream->buf += QString::number(a); return *this; }
inline LogMessage arg(long a)
{ stream->buf = stream->buf.arg(a); return *this; }
inline LogMessage &operator<<(ulong a)
{ stream->buf += QString::number(a); return *this; }
inline LogMessage arg(ulong a)
{ stream->buf = stream->buf.arg(a); return *this; }
inline LogMessage &operator<<(qlonglong a)
{ stream->buf += QString::number(a); return *this; }
inline LogMessage arg(qlonglong a)
{ stream->buf = stream->buf.arg(a); return *this; }
inline LogMessage &operator<<(qulonglong a)
{ stream->buf += QString::number(a); return *this; }
inline LogMessage arg(qulonglong a)
{ stream->buf = stream->buf.arg(a); return *this; }
};
#endif

View File

@ -1,124 +0,0 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2006-2007, crypton
* Copyright (c) 2006, Matt Edman, Justin Hipple
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#include <QDir>
#include <QFile>
#include <QTextStream>
#include "string.h"
#include "process.h"
/** Returns the PID of the current process. */
qint64
get_pid()
{
#if defined(Q_OS_WIN)
return (qint64)GetCurrentProcessId();
#else
return (qint64)getpid();
#endif
}
/** Returns true if a process with the given PID is running. */
bool
is_process_running(qint64 pid)
{
#if defined(Q_OS_WIN)
BOOL rc;
DWORD exitCode;
/* Try to open the process to see if it exists */
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, (DWORD)pid);
if (hProcess == NULL) {
return false;
}
/* It exists, so see if it's still active or if it terminated already */
rc = GetExitCodeProcess(hProcess, &exitCode);
CloseHandle(hProcess);
if (!rc) {
/* Error. Assume it doesn't exist (is this a bad assumption?) */
return false;
}
/* If GetExitCodeProcess() returns a non-zero value, and the process is
* still running, exitCode should equal STILL_ACTIVE. Otherwise, this means
* the process has terminated. */
return (exitCode == STILL_ACTIVE);
#else
/* Send the "null" signal to check if a process exists */
if (kill((pid_t)pid, 0) < 0) {
return (errno != ESRCH);
}
return true;
#endif
}
/** Writes the given file to disk containing the current process's PID. */
bool
write_pidfile(QString pidFileName, QString *errmsg)
{
/* Make sure the directory exists */
QDir pidFileDir = QFileInfo(pidFileName).absoluteDir();
if (!pidFileDir.exists()) {
pidFileDir.mkpath(QDir::convertSeparators(pidFileDir.absolutePath()));
}
/* Try to open (and create if it doesn't exist) the pidfile */
QFile pidfile(pidFileName);
if (!pidfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
return err(errmsg, pidfile.errorString());
}
/* Write our current PID to it */
QTextStream pidstream(&pidfile);
pidstream << get_pid();
return true;
}
/** Reads the given pidfile and returns the value contained in it. If the file
* does not exist 0 is returned. Returns -1 if an error occurs. */
qint64
read_pidfile(QString pidFileName, QString *errmsg)
{
qint64 pid;
/* Open the pidfile, if it exists */
QFile pidfile(pidFileName);
if (!pidfile.exists()) {
return 0;
}
if (!pidfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
if (errmsg) {
*errmsg = pidfile.errorString();
}
return -1;
}
/* Read the PID in from the file */
QTextStream pidstream(&pidfile);
pidstream >> pid;
return pid;
}

View File

@ -1,54 +0,0 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2006-2007, crypton
* Copyright (c) 2006, Matt Edman, Justin Hipple
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#ifndef _PROCESS_H
#define _PROCESS_H
#include <QString>
#if defined(Q_OS_WIN)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#endif
/** Returns the PID of the current process. */
qint64 get_pid();
/** Returns true if a process with the given PID is running. */
bool is_process_running(qint64 pid);
/** Writes the given file to disk containing the current process's PID. */
bool write_pidfile(QString pidfile, QString *errmsg = 0);
/** Reads the giiven pidfile and returns the value in it. If the file does not
* exist, -1 is returned. */
qint64 read_pidfile(QString pidfile, QString *errmsg = 0);
#endif

View File

@ -1,111 +0,0 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2006-2007, crypton
* Copyright (c) 2006, Matt Edman, Justin Hipple
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#include "registry.h"
/** Returns the value in keyName at keyLocation.
* Returns an empty QString if the keyName doesn't exist */
QString
registry_get_key_value(QString keyLocation, QString keyName)
{
#if 0
HKEY key;
char data[255] = {0};
DWORD size = sizeof(data);
/* Open the key for reading (opens new key if it doesn't exist) */
if (RegOpenKeyExA(HKEY_CURRENT_USER,
qPrintable(keyLocation),
0L, KEY_READ, &key) == ERROR_SUCCESS) {
/* Key exists, so read the value into data */
RegQueryValueExA(key, qPrintable(keyName),
NULL, NULL, (LPBYTE)data, &size);
}
/* Close anything that was opened */
RegCloseKey(key);
return QString(data);
#endif
return keyName;
}
/** Creates and/or sets the key to the specified value */
void
registry_set_key_value(QString keyLocation, QString keyName, QString keyValue)
{
#if 0
HKEY key;
/* Open the key for writing (opens new key if it doesn't exist */
if (RegOpenKeyExA(HKEY_CURRENT_USER,
qPrintable(keyLocation),
0, KEY_WRITE, &key) != ERROR_SUCCESS) {
/* Key didn't exist, so write the newly opened key */
RegCreateKeyExA(HKEY_CURRENT_USER,
qPrintable(keyLocation),
0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&key, NULL);
}
/* Save the value in the key */
RegSetValueExA(key, qPrintable(keyName), 0, REG_SZ,
(BYTE *)qPrintable(keyValue),
(DWORD)keyValue.length() + 1); // include null terminator
/* Close the key */
RegCloseKey(key);
#endif
}
/** Removes the key from the registry if it exists */
void
registry_remove_key(QString keyLocation, QString keyName)
{
#if 0
HKEY key;
/* Open the key for writing (opens new key if it doesn't exist */
if (RegOpenKeyExA(HKEY_CURRENT_USER,
qPrintable(keyLocation),
0, KEY_SET_VALUE, &key) == ERROR_SUCCESS) {
/* Key exists so delete it */
RegDeleteValueA(key, qPrintable(keyName));
}
/* Close anything that was opened */
RegCloseKey(key);
#endif
}

View File

@ -1,47 +0,0 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2006-2007, crypton
* Copyright (c) 2006, Matt Edman, Justin Hipple
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#ifndef _REGISTRY_H
#define _REGISTRY_H
#include <QString>
#define WIN32_LEAN_AND_MEAN
#if 0
#include "windows.h"
#endif
/** Returns value of keyName or empty QString if keyName doesn't exist */
QString registry_get_key_value(QString keyLocation, QString keyName);
/** Creates and/or sets the key to the specified value */
void registry_set_key_value(QString keyLocation, QString keyName, QString keyValue);
/** Removes the key from the registry if it exists */
void registry_remove_key(QString keyLocation, QString keyName);
#endif

View File

@ -1,66 +0,0 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2006-2007, crypton
* Copyright (c) 2006, Matt Edman, Justin Hipple
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#include "string.h"
/** Create a QStringList from the array of C-style strings. */
QStringList
char_array_to_stringlist(char **arr, int len)
{
QStringList list;
for (int i = 0; i < len; i++) {
list << QString(arr[i]);
}
return list;
}
/** Conditionally assigns errmsg to str if str is not null and returns false.
* This is a seemingly pointless function, but it saves some messiness in
* methods whose QString *errmsg parameter is optional. */
bool
err(QString *str, QString errmsg)
{
if (str) {
*str = errmsg;
}
return false;
}
/** Ensures all characters in str are in validChars. If a character appears
* in str but not in validChars, it will be removed and the resulting
* string returned. */
QString
ensure_valid_chars(QString str, QString validChars)
{
QString out = str;
for (int i = 0; i < str.length(); i++) {
QChar c = str.at(i);
if (validChars.indexOf(c) < 0) {
out.remove(c);
}
}
return out;
}

View File

@ -1,43 +0,0 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2006-2007, crypton
* Copyright (c) 2006, Matt Edman, Justin Hipple
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#ifndef __STRING_H
#define __STRING_H
#include <QStringList>
/** Creates a QStringList from the array of C strings. */
QStringList char_array_to_stringlist(char **arr, int len);
/** Ensures all characters in str are in validChars. If a character appears
* in str but not in validChars, it will be removed and the resulting
* string returned. */
QString ensure_valid_chars(QString str, QString validChars);
/** Conditionally assigns errmsg to string if str is not null and returns
* false. */
bool err(QString *str, QString errmsg);
#endif

View File

@ -0,0 +1,319 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2008, crypton
* Copyright (c) 2008, Matt Edman, Justin Hipple
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
/*
** \file stringutil.cpp
** \version $Id: stringutil.cpp 2486 2008-04-05 14:43:08Z edmanm $
** \brief Common string manipulation functions
*/
#include "stringutil.h"
/** Create a QStringList from the array of C-style strings. */
QStringList
char_array_to_stringlist(char **arr, int len)
{
QStringList list;
for (int i = 0; i < len; i++) {
list << QString(arr[i]);
}
return list;
}
/** Conditionally assigns errmsg to str if str is not null and returns false.
* This is a seemingly pointless function, but it saves some messiness in
* methods whose QString *errmsg parameter is optional. */
bool
err(QString *str, const QString &errmsg)
{
if (str) {
*str = errmsg;
}
return false;
}
/** Ensures all characters in str are in validChars. If a character appears
* in str but not in validChars, it will be removed and the resulting
* string returned. */
QString
ensure_valid_chars(const QString &str, const QString &validChars)
{
QString out = str;
for (int i = 0; i < str.length(); i++) {
QChar c = str.at(i);
if (validChars.indexOf(c) < 0) {
out.remove(c);
}
}
return out;
}
/** Scrubs an email address by replacing "@" with " at " and "." with " dot ". */
QString
scrub_email_addr(const QString &email)
{
QString scrubbed = email;
scrubbed = scrubbed.replace("@", " at ");
scrubbed = scrubbed.replace(".", " dot ");
return scrubbed;
}
/** Wraps <b>str</b> at <b>width</b> characters wide, using <b>sep</b> as the
* word separator (" ", for example), and placing the line ending <b>le</b> at
* the end of each line, except the last. */
QString
string_wrap(const QString &str, int width,
const QString &sep, const QString &le)
{
QString wrapped;
int pos, nextsep, wordlen, n;
int seplen = sep.length();
if (str.length() < width) {
return str;
}
pos = 0;
n = width;
while (pos < str.length()) {
/* Get the length of a "word" */
nextsep = str.indexOf(sep, pos);
if (nextsep < 0) {
nextsep = str.length();
}
wordlen = nextsep-pos;
/* Check if there is room for the word on this line */
if (wordlen > n) {
/* Create a new line */
wrapped.append(le);
n = width;
}
/* Add the word to the current line */
wrapped.append(str.mid(pos, wordlen+seplen));
n = n - wordlen - seplen;
pos += wordlen + seplen;
}
return wrapped.trimmed();
}
/** Encodes the bytes in <b>buf</b> as an uppercase hexadecimal string and
* returns the result. This function is derived from base16_encode() in Tor's
* util.c. See LICENSE for details on Tor's license. */
QString
base16_encode(const QByteArray &buf)
{
QString hex;
for (int i = 0; i < buf.size(); i++) {
hex += "0123456789ABCDEF"[((quint8)buf[i]) >> 4];
hex += "0123456789ABCDEF"[((quint8)buf[i]) & 0xf];
}
return hex;
}
/** Given a string <b>str</b>, this function returns a quoted string with all
* '"' and '\' characters escaped with a single '\'. */
QString
string_escape(const QString &str)
{
QString out;
out.append("\"");
for (int i = 0; i < str.length(); i++) {
if (str[i] == '\"' || str[i] == '\\')
out.append('\\');
out.append(str[i]);
}
out.append("\"");
return out;
}
/** Given a quoted string <b>str</b>, this function returns an unquoted,
* unescaped string. <b>str</b> must start and end with an unescaped quote. */
QString
string_unescape(const QString &str, bool *ok)
{
QString out;
/* The string must start and end with an unescaped dquote */
if (str.length() < 2 || !str.startsWith("\"") || !str.endsWith("\"") ||
(str.endsWith("\\\"") && !str.endsWith("\\\\\""))) {
if (ok)
*ok = false;
return QString();
}
for (int i = 1; i < str.length()-1; i++) {
if (str[i] == '\\')
i++;
out.append(str[i]);
}
if (ok)
*ok = true;
return out;
}
/** Parses a series of space-separated key[=value|="value"] tokens from
* <b>str</b> and returns the mappings in a QHash. If <b>str</b> was unable
* to be parsed, <b>ok</b> is set to false. */
QHash<QString,QString>
string_parse_keyvals(const QString &str, bool *ok)
{
int i, len;
bool tmp_ok;
QHash<QString,QString> keyvals;
i = 0;
len = str.length();
while (i < len && str[i].isSpace())
i++; /* Skip initial whitespace */
while (i < len) {
QString key, val;
while (i < len && !str[i].isSpace() && str[i] != '=')
key.append(str[i++]);
if (i < len && str[i] == '=') {
if (++i < len && str[i] == '\"') {
/* The value is wrapped in quotes */
val.append(str[i]);
while (++i < len) {
val.append(str[i]);
if (str[i] == '\\') {
if (++i == len)
goto error;
val.append(str[i]);
} else if (str[i] == '\"') {
i++;
break;
}
}
val = string_unescape(val, &tmp_ok);
if (!tmp_ok)
goto error;
keyvals.insert(key, val);
} else {
/* The value was not wrapped in quotes */
while (i < len && !str[i].isSpace())
val.append(str[i++]);
keyvals.insert(key, val);
}
} else {
/* The key had no value */
keyvals.insert(key, QString(""));
}
while (i < len && str[i].isSpace())
i++;
}
if (ok)
*ok = true;
return keyvals;
error:
if (ok)
*ok = false;
return QHash<QString,QString>();
}
/** Parses a series of command line arguments from <b>str</b>. If <b>str</b>
* was unable to be parsed, <b>ok</b> is set to false. */
QStringList
string_parse_arguments(const QString &str, bool *ok)
{
QStringList args;
int i, len;
bool tmp_ok;
i = 0;
len = str.length();
while (i < len && str[i].isSpace())
i++; /* Skip initial whitespace */
while (i < len) {
QString arg;
if (str[i] == '\"') {
/* The value is wrapped in quotes */
arg.append(str[i]);
while (++i < len) {
arg.append(str[i]);
if (str[i] == '\\') {
if (++i == len)
goto error;
arg.append(str[i]);
} else if (str[i] == '\"') {
i++;
break;
}
}
arg = string_unescape(arg, &tmp_ok);
if (!tmp_ok)
goto error;
args << arg;
} else {
/* The value was not wrapped in quotes */
while (i < len && !str[i].isSpace())
arg.append(str[i++]);
args << arg;
}
while (i < len && str[i].isSpace())
i++;
}
if (ok)
*ok = true;
return args;
error:
if (ok)
*ok = false;
return QStringList();
}
/** Formats the list of command line arguments in <b>args</b> as a string.
* Arguments that contain ' ', '\', or '"' tokens will be escaped and
* wrapped in double quotes. */
QString
string_format_arguments(const QStringList &args)
{
QStringList out;
foreach (QString arg, args) {
if (arg.contains("\"") || arg.contains("\\") || arg.contains(" "))
out << string_escape(arg);
else
out << arg;
}
return out.join(" ");
}
/** Returns true if <b>str</b> is a valid hexademical string. Returns false
* otherwise. */
bool
string_is_hex(const QString &str)
{
for (int i = 0; i < str.length(); i++) {
char c = str[i].toUpper().toAscii();
if ((c < 'A' || c > 'F') && (c < '0' || c > '9'))
return false;
}
return true;
}

View File

@ -0,0 +1,88 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2008, crypton
* Copyright (c) 2008, Matt Edman, Justin Hipple
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
/*
** \file stringutil.h
** \version $Id: stringutil.h 2486 2008-04-05 14:43:08Z edmanm $
** \brief Common string manipulation functions
*/
#ifndef _STRINGUTIL_H
#define _STRINGUTIL_H
#include <QStringList>
#include <QHash>
/** Creates a QStringList from the array of C strings. */
QStringList char_array_to_stringlist(char **arr, int len);
/** Ensures all characters in str are in validChars. If a character appears
* in str but not in validChars, it will be removed and the resulting
* string returned. */
QString ensure_valid_chars(const QString &str, const QString &validChars);
/** Scrubs an email address by replacing "@" with " at " and "." with " dot ". */
QString scrub_email_addr(const QString &email);
/** Conditionally assigns errmsg to string if str is not null and returns
* false. */
bool err(QString *str, const QString &errmsg);
/** Wraps <b>str</b> at <b>width</b> characters wide, using <b>sep</b> as the
* word separator (" ", for example), and placing the line ending <b>le</b> at
* the end of each line, except the last.*/
QString string_wrap(const QString &str, int width,
const QString &sep, const QString &le);
/** Encodes the bytes in <b>buf</b> as an uppercase hexadecimal string and
* returns the result. This function is derived from base16_encode() in Tor's
* util.c. See LICENSE for details on Tor's license. */
QString base16_encode(const QByteArray &buf);
/** Given a string <b>str</b>, this function returns a quoted string with all
* '"' and '\' characters escaped with a single '\'. */
QString string_escape(const QString &str);
/** Given a quoted string <b>str</b>, this function returns an unquoted,
* unescaped string. <b>str</b> must start and end with an unescaped quote. */
QString string_unescape(const QString &str, bool *ok = 0);
/** Parses a series of space-separated key[=value|="value"] tokens from
* <b>str</b> and returns the mappings in a QHash. If <b>str</b> was unable
* to be parsed, <b>ok</b> is set to false. */
QHash<QString,QString> string_parse_keyvals(const QString &str, bool *ok = 0);
/** Parses a series of command line arguments from <b>str</b>. If <b>str</b>
* was unable to be parsed, <b>ok</b> is set to false. */
QStringList string_parse_arguments(const QString &str, bool *ok = 0);
/** Formats the list of command line arguments in <b>args</b> as a string.
* Arguments that contain ' ', '\', or '"' tokens will be escaped and wrapped
* in double quotes. */
QString string_format_arguments(const QStringList &args);
/** Returns true if <b>str</b> is a valid hexademical string. Returns false
* otherwise. */
bool string_is_hex(const QString &str);
#endif