1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/widget/form/settingsform.cpp

225 lines
8.4 KiB
C++
Raw Normal View History

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"
#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>
#include <QClipboard>
#include <QApplication>
2014-07-25 20:52:14 +08:00
#include <QFileDialog>
#include <QDir>
#include <QMessageBox>
#include "core.h"
2014-06-25 04:11:11 +08:00
SettingsForm::SettingsForm()
: QObject()
2014-06-25 04:11:11 +08:00
{
main = new QWidget(), head = new QWidget();
hboxcont1 = new QWidget(), hboxcont2 = new QWidget();
2014-06-25 04:11:11 +08:00
QFont bold, small;
bold.setBold(true);
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-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);
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);
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());
makeToxPortable.setToolTip(tr("Save settings to the working directory instead of the usual conf dir","describes makeToxPortable checkbox"));
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-25 04:11:11 +08:00
main->setLayout(&layout);
layout.addWidget(&idLabel);
layout.addWidget(&id);
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);
layout.addWidget(&importConf);
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);
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()));
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()));
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()
{
}
QList<QString> SettingsForm::searchProfiles()
{
QList<QString> out;
QDir dir(Settings::getSettingsDirPath());
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
dir.setNameFilters(QStringList("*.tox"));
for(QFileInfo file : dir.entryInfoList())
{
out += file.completeBaseName();
}
return out;
}
QString SettingsForm::getSelectedSavePath()
{
return Settings::getSettingsDirPath() + QDir::separator() + profiles.currentText() + Core::getInstance()->TOX_EXT;
}
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
}
void SettingsForm::show(Ui::MainWindow &ui)
2014-06-25 04:11:11 +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();
}
void SettingsForm::onLoadClicked()
{
Core::getInstance()->switchConfiguration(profiles.currentText());
}
void SettingsForm::onExportClicked()
{
QString current = getSelectedSavePath();
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"));
QFile::copy(getSelectedSavePath(), path);
}
void SettingsForm::onDeleteClicked()
{
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());
}
}
}
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();
QString profilePath = Settings::getSettingsDirPath() + profile + Core::getInstance()->TOX_EXT;
Settings::getInstance().setCurrentProfile(profile);
QFile::copy(path, profilePath);
profiles.addItem(profile);
Core::getInstance()->switchConfiguration(profile);
}
void SettingsForm::onTestVideoClicked()
{
Widget::getInstance()->showTestCamview();
}
2014-07-02 06:47:06 +08:00
void SettingsForm::onEnableIPv6Updated()
{
Settings::getInstance().setEnableIPv6(enableIPv6.isChecked());
}
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 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
}