2014-07-07 00:19:45 +08:00
/*
Copyright ( C ) 2014 by Project Tox < https : //tox.im>
This file is part of qTox , a Qt - based graphical interface for Tox .
This program is libre 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 3 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 COPYING file for more details .
*/
2014-06-25 04:11:11 +08:00
# include "settingsform.h"
2014-06-30 05:41:47 +08:00
# include "widget/widget.h"
2014-07-02 06:47:06 +08:00
# include "settings.h"
2014-07-30 15:18:41 +08:00
# include "smileypack.h"
2014-09-11 21:44:34 +08:00
# include "ui_mainwindow.h"
2014-06-25 04:11:11 +08:00
# include <QFont>
2014-07-05 00:49:19 +08:00
# include <QClipboard>
# include <QApplication>
2014-07-25 20:52:14 +08:00
# include <QFileDialog>
# include <QDir>
2014-09-13 02:53:29 +08:00
# include <QMessageBox>
# include "core.h"
2014-06-25 04:11:11 +08:00
2014-07-23 17:22:09 +08:00
SettingsForm : : SettingsForm ( )
: QObject ( )
2014-06-25 04:11:11 +08:00
{
main = new QWidget ( ) , head = new QWidget ( ) ;
2014-07-17 06:43:30 +08:00
hboxcont1 = new QWidget ( ) , hboxcont2 = new QWidget ( ) ;
2014-06-25 04:11:11 +08:00
QFont bold , small ;
bold . setBold ( true ) ;
2014-07-05 00:49:19 +08:00
small . setPixelSize ( 13 ) ;
2014-09-11 04:23:49 +08:00
small . setKerning ( false ) ;
2014-07-04 03:42:23 +08:00
headLabel . setText ( tr ( " User Settings " , " \" Headline \" of the window " ) ) ;
2014-06-25 04:11:11 +08:00
headLabel . setFont ( bold ) ;
2014-07-17 06:43:30 +08:00
2014-07-06 17:51:01 +08:00
idLabel . setText ( " Tox ID " + tr ( " (click here to copy) " , " Click on this text to copy TID to clipboard " ) ) ;
2014-06-25 04:11:11 +08:00
id . setFont ( small ) ;
id . setTextInteractionFlags ( Qt : : TextSelectableByMouse ) ;
2014-07-05 00:49:19 +08:00
id . setReadOnly ( true ) ;
id . setFrameStyle ( QFrame : : NoFrame ) ;
2014-07-05 01:22:43 +08:00
id . setVerticalScrollBarPolicy ( Qt : : ScrollBarAlwaysOff ) ;
2014-09-11 04:23:49 +08:00
id . setFixedHeight ( id . document ( ) - > size ( ) . height ( ) * 2 ) ;
2014-07-17 06:43:30 +08:00
profilesLabel . setText ( tr ( " Available profiles: " , " Labels the profile selection box " ) ) ;
loadConf . setText ( tr ( " Load profile " , " button to load selected profile " ) ) ;
exportConf . setText ( tr ( " Export profile " , " button to save selected profile elsewhere " ) ) ;
delConf . setText ( tr ( " Delete profile " , " button to delete selected profile from disk " ) ) ;
delConf . setToolTip ( tr ( " This is useful to remain safe on public computers " , " describes the delete profile button " ) ) ;
//delConf.setWhatsThis(tr("This is useful to remain safe on public computers", "describes the delete profile button"));
importConf . setText ( tr ( " Import profile " , " button to locate a profile " ) ) ;
2014-06-25 04:11:11 +08:00
2014-07-04 03:42:23 +08:00
videoTest . setText ( tr ( " Test video " , " Text on a button to test the video/webcam " ) ) ;
enableIPv6 . setText ( tr ( " Enable IPv6 (recommended) " , " Text on a checkbox to enable IPv6 " ) ) ;
2014-07-02 06:47:06 +08:00
enableIPv6 . setChecked ( Settings : : getInstance ( ) . getEnableIPv6 ( ) ) ;
2014-07-05 01:22:43 +08:00
useTranslations . setText ( tr ( " Use translations " , " Text on a checkbox to enable translations " ) ) ;
useTranslations . setChecked ( Settings : : getInstance ( ) . getUseTranslations ( ) ) ;
2014-07-13 04:58:58 +08:00
makeToxPortable . setText ( tr ( " Make Tox portable " , " Text on a checkbox to make qTox a portable application " ) ) ;
makeToxPortable . setChecked ( Settings : : getInstance ( ) . getMakeToxPortable ( ) ) ;
2014-07-28 04:55:22 +08:00
makeToxPortable . setToolTip ( tr ( " Save settings to the working directory instead of the usual conf dir " , " describes makeToxPortable checkbox " ) ) ;
2014-06-29 04:11:42 +08:00
2014-07-25 20:52:14 +08:00
smileyPackLabel . setText ( tr ( " Smiley Pack " , " Text on smiley pack label " ) ) ;
2014-08-01 20:46:28 +08:00
for ( auto entry : SmileyPack : : listSmileyPacks ( ) )
2014-07-31 23:36:55 +08:00
smileyPackBrowser . addItem ( entry . first , entry . second ) ;
smileyPackBrowser . setCurrentIndex ( smileyPackBrowser . findData ( Settings : : getInstance ( ) . getSmileyPack ( ) ) ) ;
2014-06-29 04:11:42 +08:00
2014-06-25 04:11:11 +08:00
main - > setLayout ( & layout ) ;
layout . addWidget ( & idLabel ) ;
layout . addWidget ( & id ) ;
2014-07-17 06:43:30 +08:00
cbox . addWidget ( & profilesLabel ) ;
cbox . addWidget ( & profiles ) ;
hboxcont1 - > setLayout ( & cbox ) ;
layout . addWidget ( hboxcont1 ) ;
buttons . addWidget ( & loadConf ) ;
buttons . addWidget ( & exportConf ) ;
buttons . addWidget ( & delConf ) ;
hboxcont2 - > setLayout ( & buttons ) ;
layout . addWidget ( hboxcont2 ) ;
2014-07-17 06:46:51 +08:00
layout . addWidget ( & importConf ) ;
2014-06-29 04:11:42 +08:00
layout . addWidget ( & videoTest ) ;
2014-07-02 06:47:06 +08:00
layout . addWidget ( & enableIPv6 ) ;
2014-07-05 01:22:43 +08:00
layout . addWidget ( & useTranslations ) ;
2014-07-13 04:58:58 +08:00
layout . addWidget ( & makeToxPortable ) ;
2014-07-25 20:52:14 +08:00
layout . addWidget ( & smileyPackLabel ) ;
2014-07-30 15:18:41 +08:00
layout . addWidget ( & smileyPackBrowser ) ;
2014-06-25 04:11:11 +08:00
layout . addStretch ( ) ;
head - > setLayout ( & headLayout ) ;
headLayout . addWidget ( & headLabel ) ;
2014-06-29 04:11:42 +08:00
2014-07-17 09:00:23 +08:00
connect ( & loadConf , SIGNAL ( clicked ( ) ) , this , SLOT ( onLoadClicked ( ) ) ) ;
connect ( & exportConf , SIGNAL ( clicked ( ) ) , this , SLOT ( onExportClicked ( ) ) ) ;
connect ( & delConf , SIGNAL ( clicked ( ) ) , this , SLOT ( onDeleteClicked ( ) ) ) ;
connect ( & importConf , SIGNAL ( clicked ( ) ) , this , SLOT ( onImportClicked ( ) ) ) ;
2014-06-29 04:11:42 +08:00
connect ( & videoTest , SIGNAL ( clicked ( ) ) , this , SLOT ( onTestVideoClicked ( ) ) ) ;
2014-07-02 06:47:06 +08:00
connect ( & enableIPv6 , SIGNAL ( stateChanged ( int ) ) , this , SLOT ( onEnableIPv6Updated ( ) ) ) ;
2014-07-06 17:51:01 +08:00
connect ( & useTranslations , SIGNAL ( stateChanged ( int ) ) , this , SLOT ( onUseTranslationUpdated ( ) ) ) ;
2014-07-13 04:58:58 +08:00
connect ( & makeToxPortable , SIGNAL ( stateChanged ( int ) ) , this , SLOT ( onMakeToxPortableUpdated ( ) ) ) ;
2014-07-05 00:49:19 +08:00
connect ( & idLabel , SIGNAL ( clicked ( ) ) , this , SLOT ( copyIdClicked ( ) ) ) ;
2014-07-31 23:36:55 +08:00
connect ( & smileyPackBrowser , SIGNAL ( currentIndexChanged ( int ) ) , this , SLOT ( onSmileyBrowserIndexChanged ( int ) ) ) ;
2014-06-25 04:11:11 +08:00
}
SettingsForm : : ~ SettingsForm ( )
{
}
2014-07-29 05:52:05 +08:00
QList < QString > SettingsForm : : searchProfiles ( )
2014-07-17 06:43:30 +08:00
{
2014-07-29 05:52:05 +08:00
QList < QString > out ;
2014-07-17 06:43:30 +08:00
QDir dir ( Settings : : getSettingsDirPath ( ) ) ;
dir . setFilter ( QDir : : Files | QDir : : NoDotAndDotDot ) ;
dir . setNameFilters ( QStringList ( " *.tox " ) ) ;
2014-07-29 05:52:05 +08:00
for ( QFileInfo file : dir . entryInfoList ( ) )
{
out + = file . completeBaseName ( ) ;
2014-07-17 06:43:30 +08:00
}
2014-07-29 05:52:05 +08:00
return out ;
2014-07-17 06:43:30 +08:00
}
2014-07-17 09:00:23 +08:00
QString SettingsForm : : getSelectedSavePath ( )
{
2014-09-13 02:53:29 +08:00
return Settings : : getSettingsDirPath ( ) + QDir : : separator ( ) + profiles . currentText ( ) + Core : : getInstance ( ) - > TOX_EXT ;
2014-07-17 09:00:23 +08:00
}
2014-06-25 04:11:11 +08:00
void SettingsForm : : setFriendAddress ( const QString & friendAddress )
{
2014-09-11 04:23:49 +08:00
QString txt { friendAddress } ;
txt . insert ( 38 , ' \n ' ) ;
id . setText ( txt ) ;
2014-06-25 04:11:11 +08:00
}
2014-08-11 16:00:08 +08:00
void SettingsForm : : show ( Ui : : MainWindow & ui )
2014-06-25 04:11:11 +08:00
{
2014-07-29 05:52:05 +08:00
profiles . clear ( ) ;
for ( QString profile : searchProfiles ( ) )
{
profiles . addItem ( profile ) ;
}
2014-06-25 04:11:11 +08:00
ui . mainContent - > layout ( ) - > addWidget ( main ) ;
ui . mainHead - > layout ( ) - > addWidget ( head ) ;
main - > show ( ) ;
head - > show ( ) ;
}
2014-06-29 04:11:42 +08:00
2014-07-17 09:00:23 +08:00
void SettingsForm : : onLoadClicked ( )
{
2014-09-13 02:53:29 +08:00
Core : : getInstance ( ) - > switchConfiguration ( profiles . currentText ( ) ) ;
2014-07-17 09:00:23 +08:00
}
void SettingsForm : : onExportClicked ( )
{
QString current = getSelectedSavePath ( ) ;
2014-09-13 02:53:29 +08:00
QString path = QFileDialog : : getSaveFileName ( 0 , tr ( " Export profile " , " save dialog title " ) , QDir : : homePath ( ) + QDir : : separator ( ) + profiles . currentText ( ) + Core : : getInstance ( ) - > TOX_EXT , tr ( " Tox save file (*.tox) " , " save dialog filter " ) ) ;
2014-07-17 09:00:23 +08:00
QFile : : copy ( getSelectedSavePath ( ) , path ) ;
}
void SettingsForm : : onDeleteClicked ( )
2014-07-23 17:22:09 +08:00
{
if ( Settings : : getInstance ( ) . getCurrentProfile ( ) = = profiles . currentText ( ) )
{
QMessageBox : : warning ( main , tr ( " Profile currently loaded " , " current profile deletion warning title " ) , tr ( " This profile is currently in use. Please load a different profile before deleting this one. " , " current profile deletion warning text " ) ) ;
}
else
{
QMessageBox : : StandardButton resp = QMessageBox : : question ( main ,
tr ( " Deletion imminent! " , " deletion confirmation title " ) , tr ( " Are you sure you want to delete this profile? " , " deletion confirmation text " ) , QMessageBox : : Yes | QMessageBox : : No , QMessageBox : : No ) ;
if ( resp = = QMessageBox : : Yes )
{
QFile : : remove ( getSelectedSavePath ( ) ) ;
profiles . removeItem ( profiles . currentIndex ( ) ) ;
}
}
2014-07-17 09:00:23 +08:00
}
void SettingsForm : : onImportClicked ( )
{
QString path = QFileDialog : : getOpenFileName ( 0 , tr ( " Import profile " , " import dialog title " ) , QDir : : homePath ( ) , tr ( " Tox save file (*.tox) " , " import dialog filter " ) ) ;
QFileInfo info ( path ) ;
QString profile = info . completeBaseName ( ) ;
2014-09-13 02:53:29 +08:00
QString profilePath = Settings : : getSettingsDirPath ( ) + profile + Core : : getInstance ( ) - > TOX_EXT ;
2014-07-27 16:27:15 +08:00
Settings : : getInstance ( ) . setCurrentProfile ( profile ) ;
2014-07-17 09:00:23 +08:00
QFile : : copy ( path , profilePath ) ;
2014-07-23 17:22:09 +08:00
profiles . addItem ( profile ) ;
2014-09-13 02:53:29 +08:00
Core : : getInstance ( ) - > switchConfiguration ( profile ) ;
2014-07-17 09:00:23 +08:00
}
2014-06-29 04:11:42 +08:00
void SettingsForm : : onTestVideoClicked ( )
{
2014-06-30 05:41:47 +08:00
Widget : : getInstance ( ) - > showTestCamview ( ) ;
2014-06-29 04:11:42 +08:00
}
2014-07-02 06:47:06 +08:00
void SettingsForm : : onEnableIPv6Updated ( )
{
Settings : : getInstance ( ) . setEnableIPv6 ( enableIPv6 . isChecked ( ) ) ;
}
2014-07-05 00:49:19 +08:00
void SettingsForm : : copyIdClicked ( )
{
2014-09-11 04:27:56 +08:00
id . selectAll ( ) ;
QString txt = id . toPlainText ( ) ;
txt . replace ( ' \n ' , " " ) ;
QApplication : : clipboard ( ) - > setText ( txt ) ;
2014-07-05 00:49:19 +08:00
}
2014-07-05 01:22:43 +08:00
void SettingsForm : : onUseTranslationUpdated ( )
{
Settings : : getInstance ( ) . setUseTranslations ( useTranslations . isChecked ( ) ) ;
}
2014-07-13 04:58:58 +08:00
void SettingsForm : : onMakeToxPortableUpdated ( )
{
Settings : : getInstance ( ) . setMakeToxPortable ( makeToxPortable . isChecked ( ) ) ;
}
2014-07-25 20:52:14 +08:00
2014-07-31 23:36:55 +08:00
void SettingsForm : : onSmileyBrowserIndexChanged ( int index )
2014-07-25 20:52:14 +08:00
{
2014-07-31 23:36:55 +08:00
QString filename = smileyPackBrowser . itemData ( index ) . toString ( ) ;
2014-07-30 15:18:41 +08:00
Settings : : getInstance ( ) . setSmileyPack ( filename ) ;
2014-07-25 20:52:14 +08:00
}