2014-09-15 18:45:59 +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-10-08 12:26:25 +08:00
# include "src/core.h"
2014-10-06 00:17:01 +08:00
# include "ui_identitysettings.h"
2014-09-15 18:45:59 +08:00
# include "identityform.h"
2014-10-08 12:26:25 +08:00
# include "src/widget/form/settingswidget.h"
2014-10-12 17:58:56 +08:00
# include "src/misc/settings.h"
2014-10-08 12:26:25 +08:00
# include "src/widget/croppinglabel.h"
2014-10-12 17:58:56 +08:00
# include "src/widget/widget.h"
2014-09-15 18:45:59 +08:00
# include <QLabel>
# include <QLineEdit>
2014-09-30 17:44:27 +08:00
# include <QApplication>
# include <QClipboard>
2014-10-09 16:07:26 +08:00
# include <QInputDialog>
# include <QFileDialog>
# include <QMessageBox>
2014-09-15 18:45:59 +08:00
2014-10-06 00:17:01 +08:00
IdentityForm : : IdentityForm ( ) :
GenericForm ( tr ( " Your identity " ) , QPixmap ( " :/img/settings/identity.png " ) )
2014-09-15 18:45:59 +08:00
{
2014-10-06 00:17:01 +08:00
bodyUI = new Ui : : IdentitySettings ;
bodyUI - > setupUi ( this ) ;
2014-09-30 17:44:27 +08:00
// tox
toxId = new ClickableTE ( ) ;
QFont small ;
small . setPixelSize ( 13 ) ;
small . setKerning ( false ) ;
2014-10-06 00:17:01 +08:00
2014-10-06 02:02:12 +08:00
// toxId->setTextInteractionFlags(Qt::TextSelectableByMouse);
2014-09-30 17:44:27 +08:00
toxId - > setReadOnly ( true ) ;
2014-10-09 16:59:35 +08:00
toxId - > setFrame ( false ) ;
2014-10-06 02:02:12 +08:00
// toxId->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// toxId->setFixedHeight(toxId->document()->size().height()*2);
2014-09-30 17:44:27 +08:00
toxId - > setFont ( small ) ;
2014-10-06 00:17:01 +08:00
bodyUI - > toxGroup - > layout ( ) - > addWidget ( toxId ) ;
2014-09-30 17:44:27 +08:00
2014-10-06 00:17:01 +08:00
connect ( bodyUI - > toxIdLabel , SIGNAL ( clicked ( ) ) , this , SLOT ( copyIdClicked ( ) ) ) ;
2014-09-30 17:44:27 +08:00
connect ( toxId , SIGNAL ( clicked ( ) ) , this , SLOT ( copyIdClicked ( ) ) ) ;
2014-10-06 00:17:01 +08:00
connect ( bodyUI - > userName , SIGNAL ( editingFinished ( ) ) , this , SLOT ( onUserNameEdited ( ) ) ) ;
connect ( bodyUI - > statusMessage , SIGNAL ( editingFinished ( ) ) , this , SLOT ( onStatusMessageEdited ( ) ) ) ;
2014-10-09 16:07:26 +08:00
connect ( bodyUI - > loadButton , & QPushButton : : clicked , this , & IdentityForm : : onLoadClicked ) ;
connect ( bodyUI - > renameButton , & QPushButton : : clicked , this , & IdentityForm : : onRenameClicked ) ;
connect ( bodyUI - > exportButton , & QPushButton : : clicked , this , & IdentityForm : : onExportClicked ) ;
connect ( bodyUI - > deleteButton , & QPushButton : : clicked , this , & IdentityForm : : onDeleteClicked ) ;
connect ( bodyUI - > importButton , & QPushButton : : clicked , this , & IdentityForm : : onImportClicked ) ;
2014-09-15 18:45:59 +08:00
}
IdentityForm : : ~ IdentityForm ( )
{
}
2014-09-30 17:44:27 +08:00
void IdentityForm : : copyIdClicked ( )
{
toxId - > selectAll ( ) ;
2014-10-06 02:02:12 +08:00
QString txt = toxId - > text ( ) ;
2014-09-30 17:44:27 +08:00
txt . replace ( ' \n ' , " " ) ;
QApplication : : clipboard ( ) - > setText ( txt ) ;
2014-10-09 16:59:35 +08:00
toxId - > setCursorPosition ( 0 ) ;
2014-09-30 17:44:27 +08:00
}
void IdentityForm : : onUserNameEdited ( )
{
2014-10-06 00:30:31 +08:00
Core : : getInstance ( ) - > setUsername ( bodyUI - > userName - > text ( ) ) ;
2014-09-30 17:44:27 +08:00
}
void IdentityForm : : onStatusMessageEdited ( )
{
2014-10-06 00:30:31 +08:00
Core : : getInstance ( ) - > setStatusMessage ( bodyUI - > statusMessage - > text ( ) ) ;
2014-09-30 17:44:27 +08:00
}
2014-10-08 20:25:32 +08:00
void IdentityForm : : present ( )
2014-09-30 17:44:27 +08:00
{
toxId - > setText ( Core : : getInstance ( ) - > getSelfId ( ) . toString ( ) ) ;
2014-10-09 16:59:35 +08:00
toxId - > setCursorPosition ( 0 ) ;
2014-10-09 16:07:26 +08:00
bodyUI - > profiles - > clear ( ) ;
for ( QString profile : Widget : : searchProfiles ( ) )
bodyUI - > profiles - > addItem ( profile ) ;
QString current = Settings : : getInstance ( ) . getCurrentProfile ( ) ;
if ( current ! = " " )
bodyUI - > profiles - > setCurrentText ( current ) ;
2014-09-30 17:44:27 +08:00
}
2014-10-06 00:17:01 +08:00
void IdentityForm : : setUserName ( const QString & name )
{
bodyUI - > userName - > setText ( name ) ;
}
void IdentityForm : : setStatusMessage ( const QString & msg )
{
bodyUI - > statusMessage - > setText ( msg ) ;
}
2014-10-09 16:07:26 +08:00
void IdentityForm : : onLoadClicked ( )
{
2014-10-15 09:04:53 +08:00
if ( bodyUI - > profiles - > currentText ( ) ! = Settings : : getInstance ( ) . getCurrentProfile ( ) )
{
if ( Core : : getInstance ( ) - > anyActiveCalls ( ) )
QMessageBox : : warning ( this , tr ( " Call active " , " popup title " ) ,
tr ( " You can't switch profiles while a call is active! " , " popup text " ) ) ;
else
2014-10-15 10:06:44 +08:00
emit Widget : : getInstance ( ) - > changeProfile ( bodyUI - > profiles - > currentText ( ) ) ;
// I think by directly calling the function, I may have been causing thread issues
2014-10-15 09:04:53 +08:00
}
2014-10-09 16:07:26 +08:00
}
void IdentityForm : : onRenameClicked ( )
{
QString cur = bodyUI - > profiles - > currentText ( ) ;
QString title = tr ( " Rename \" %1 \" " , " renaming a profile " ) . arg ( cur ) ;
QString name = QInputDialog : : getText ( this , title , title + " : " ) ;
if ( name ! = " " )
{
name = Core : : sanitize ( name ) ;
QDir dir ( Settings : : getSettingsDirPath ( ) ) ;
2014-10-09 17:26:32 +08:00
QFile : : rename ( dir . filePath ( cur + Core : : TOX_EXT ) , dir . filePath ( name + Core : : TOX_EXT ) ) ;
2014-10-09 16:07:26 +08:00
bodyUI - > profiles - > setItemText ( bodyUI - > profiles - > currentIndex ( ) , name ) ;
2014-10-09 17:26:32 +08:00
Settings : : getInstance ( ) . setCurrentProfile ( name ) ;
2014-10-09 16:07:26 +08:00
}
}
void IdentityForm : : onExportClicked ( )
{
QString current = bodyUI - > profiles - > currentText ( ) + Core : : TOX_EXT ;
QString path = QFileDialog : : getSaveFileName ( this , tr ( " Export profile " , " save dialog title " ) ,
QDir : : home ( ) . filePath ( current ) ,
tr ( " Tox save file (*.tox) " , " save dialog filter " ) ) ;
2014-10-14 05:17:42 +08:00
if ( ! path . isEmpty ( ) )
QFile : : copy ( QDir ( Settings : : getSettingsDirPath ( ) ) . filePath ( current ) , path ) ;
2014-10-09 16:07:26 +08:00
}
void IdentityForm : : onDeleteClicked ( )
{
if ( Settings : : getInstance ( ) . getCurrentProfile ( ) = = bodyUI - > profiles - > currentText ( ) )
{
QMessageBox : : warning ( this , 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 ( this ,
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 ( QDir ( Settings : : getSettingsDirPath ( ) ) . filePath ( bodyUI - > profiles - > currentText ( ) + Core : : TOX_EXT ) ) ;
bodyUI - > profiles - > removeItem ( bodyUI - > profiles - > currentIndex ( ) ) ;
bodyUI - > profiles - > setCurrentText ( Settings : : getInstance ( ) . getCurrentProfile ( ) ) ;
}
}
}
void IdentityForm : : onImportClicked ( )
{
QString path = QFileDialog : : getOpenFileName ( this , tr ( " Import profile " , " import dialog title " ) , QDir : : homePath ( ) , tr ( " Tox save file (*.tox) " , " import dialog filter " ) ) ;
2014-10-14 05:17:42 +08:00
if ( path . isEmpty ( ) )
return ;
2014-10-09 16:07:26 +08:00
QFileInfo info ( path ) ;
QString profile = info . completeBaseName ( ) ;
QString profilePath = QDir ( Settings : : getSettingsDirPath ( ) ) . filePath ( profile + Core : : TOX_EXT ) ;
QFile : : copy ( path , profilePath ) ;
bodyUI - > profiles - > addItem ( profile ) ;
Core : : getInstance ( ) - > switchConfiguration ( profile ) ;
}