2014-09-15 18:45:59 +08:00
/*
2015-06-06 09:40:08 +08:00
Copyright © 2014 - 2015 by The qTox Project
2014-09-15 18:45:59 +08:00
This file is part of qTox , a Qt - based graphical interface for Tox .
2015-06-06 09:40:08 +08:00
qTox is libre software : you can redistribute it and / or modify
2014-09-15 18:45:59 +08:00
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 .
2015-06-06 09:40:08 +08:00
qTox is distributed in the hope that it will be useful ,
2014-09-15 18:45:59 +08:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
2015-06-06 09:40:08 +08:00
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
2014-09-15 18:45:59 +08:00
2015-06-06 09:40:08 +08:00
You should have received a copy of the GNU General Public License
along with qTox . If not , see < http : //www.gnu.org/licenses/>.
2014-09-15 18:45:59 +08:00
*/
2015-04-24 08:32:09 +08:00
# include "src/core/core.h"
2015-03-04 04:29:01 +08:00
# include "src/nexus.h"
2015-03-11 23:15:53 +08:00
# include "ui_profileform.h"
2015-03-04 04:29:01 +08:00
# include "profileform.h"
2014-10-08 12:26:25 +08:00
# include "src/widget/form/settingswidget.h"
2015-03-04 04:29:01 +08:00
# include "src/widget/maskablepixmapwidget.h"
2015-06-04 23:45:20 +08:00
# include "src/widget/form/setpassworddialog.h"
2015-06-17 02:25:19 +08:00
# include "src/widget/contentlayout.h"
2015-06-06 07:44:47 +08:00
# include "src/widget/tool/croppinglabel.h"
2014-10-12 17:58:56 +08:00
# include "src/widget/widget.h"
2015-02-07 04:01:30 +08:00
# include "src/widget/gui.h"
2015-06-06 07:44:47 +08:00
# include "src/widget/style.h"
2016-04-13 05:48:19 +08:00
# include "src/widget/translator.h"
2015-06-06 07:44:47 +08:00
# include "src/persistence/profilelocker.h"
# include "src/persistence/profile.h"
2016-04-13 05:48:19 +08:00
# include "src/persistence/settings.h"
# include "src/net/toxme.h"
2014-09-15 18:45:59 +08:00
# include <QLabel>
# include <QLineEdit>
2015-03-04 04:29:01 +08:00
# include <QGroupBox>
2014-09-30 17:44:27 +08:00
# include <QApplication>
# include <QClipboard>
2014-10-09 16:07:26 +08:00
# include <QInputDialog>
# include <QFileDialog>
2015-03-04 04:29:01 +08:00
# include <QBuffer>
2015-06-08 02:24:55 +08:00
# include <QMessageBox>
2015-06-13 00:09:01 +08:00
# include <QComboBox>
2015-07-20 22:03:01 +08:00
# include <QWindow>
2015-10-04 19:51:01 +08:00
# include <QMenu>
# include <QMouseEvent>
2014-09-15 18:45:59 +08:00
2015-03-04 04:29:01 +08:00
ProfileForm : : ProfileForm ( QWidget * parent ) :
2015-05-10 04:35:58 +08:00
QWidget { parent } , qr { nullptr }
2014-09-15 18:45:59 +08:00
{
2014-10-06 00:17:01 +08:00
bodyUI = new Ui : : IdentitySettings ;
bodyUI - > setupUi ( this ) ;
2014-10-23 20:41:47 +08:00
core = Core : : getInstance ( ) ;
2014-09-30 17:44:27 +08:00
2015-03-11 10:01:13 +08:00
head = new QWidget ( this ) ;
QHBoxLayout * headLayout = new QHBoxLayout ( ) ;
head - > setLayout ( headLayout ) ;
QLabel * imgLabel = new QLabel ( ) ;
headLayout - > addWidget ( imgLabel ) ;
2015-06-06 03:37:01 +08:00
nameLabel = new QLabel ( ) ;
2015-03-05 03:35:34 +08:00
QFont bold ;
bold . setBold ( true ) ;
2015-03-11 10:01:13 +08:00
nameLabel - > setFont ( bold ) ;
headLayout - > addWidget ( nameLabel ) ;
headLayout - > addStretch ( 1 ) ;
imgLabel - > setPixmap ( QPixmap ( " :/img/settings/identity.png " ) . scaledToHeight ( 40 , Qt : : SmoothTransformation ) ) ;
2015-03-05 03:35:34 +08:00
2014-09-30 17:44:27 +08:00
// tox
toxId = new ClickableTE ( ) ;
toxId - > setReadOnly ( true ) ;
2014-10-09 16:59:35 +08:00
toxId - > setFrame ( false ) ;
2014-10-15 23:45:41 +08:00
toxId - > setFont ( Style : : getFont ( Style : : Small ) ) ;
2015-03-17 05:15:34 +08:00
toxId - > setToolTip ( bodyUI - > toxId - > toolTip ( ) ) ;
2015-03-17 03:53:35 +08:00
2015-03-15 23:45:28 +08:00
QVBoxLayout * toxIdGroup = qobject_cast < QVBoxLayout * > ( bodyUI - > toxGroup - > layout ( ) ) ;
2015-10-05 06:18:34 +08:00
delete toxIdGroup - > replaceWidget ( bodyUI - > toxId , toxId ) ; // Original toxId is in heap, delete it
2015-03-15 23:45:28 +08:00
bodyUI - > toxId - > hide ( ) ;
2016-01-24 05:50:57 +08:00
/* Toxme section init */
bodyUI - > toxmeServersList - > addItem ( " toxme.io " ) ;
QString toxmeInfo = Settings : : getInstance ( ) . getToxmeInfo ( ) ;
if ( toxmeInfo . isEmpty ( ) ) // User not registered
showRegisterToxme ( ) ;
else
2016-04-13 06:20:16 +08:00
showExistingToxme ( ) ;
2015-03-17 03:53:35 +08:00
2015-03-18 00:14:16 +08:00
bodyUI - > qrLabel - > setWordWrap ( true ) ;
2016-04-12 17:27:21 +08:00
QRegExp re ( " [^@ ]+ " ) ;
QRegExpValidator * validator = new QRegExpValidator ( re ) ;
bodyUI - > toxmeUsername - > setValidator ( validator ) ;
2015-02-12 20:18:04 +08:00
profilePicture = new MaskablePixmapWidget ( this , QSize ( 64 , 64 ) , " :/img/avatar_mask.svg " ) ;
2015-03-18 23:51:58 +08:00
profilePicture - > setPixmap ( QPixmap ( " :/img/contact_dark.svg " ) ) ;
2015-10-04 19:51:01 +08:00
profilePicture - > setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
2015-03-04 04:29:01 +08:00
profilePicture - > setClickable ( true ) ;
2015-10-04 19:51:01 +08:00
profilePicture - > installEventFilter ( this ) ;
2015-03-04 04:29:01 +08:00
connect ( profilePicture , SIGNAL ( clicked ( ) ) , this , SLOT ( onAvatarClicked ( ) ) ) ;
2015-10-04 19:51:01 +08:00
connect ( profilePicture , SIGNAL ( customContextMenuRequested ( const QPoint & ) ) , this , SLOT ( showProfilePictureContextMenu ( const QPoint & ) ) ) ;
2015-03-04 04:29:01 +08:00
QHBoxLayout * publicGrouplayout = qobject_cast < QHBoxLayout * > ( bodyUI - > publicGroup - > layout ( ) ) ;
publicGrouplayout - > insertWidget ( 0 , profilePicture ) ;
publicGrouplayout - > insertSpacing ( 1 , 7 ) ;
2015-01-20 16:07:56 +08:00
timer . setInterval ( 750 ) ;
2014-11-11 07:20:10 +08:00
timer . setSingleShot ( true ) ;
2015-01-20 16:07:56 +08:00
connect ( & timer , & QTimer : : timeout , this , [ = ] ( ) { bodyUI - > toxIdLabel - > setText ( bodyUI - > toxIdLabel - > text ( ) . replace ( " ✔ " , " " ) ) ; hasCheck = false ; } ) ;
2015-03-17 03:53:35 +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 ( ) ) ) ;
2015-03-04 04:29:01 +08:00
connect ( core , & Core : : idSet , this , & ProfileForm : : setToxId ) ;
2014-10-06 00:17:01 +08:00
connect ( bodyUI - > userName , SIGNAL ( editingFinished ( ) ) , this , SLOT ( onUserNameEdited ( ) ) ) ;
connect ( bodyUI - > statusMessage , SIGNAL ( editingFinished ( ) ) , this , SLOT ( onStatusMessageEdited ( ) ) ) ;
2015-03-04 04:29:01 +08:00
connect ( bodyUI - > renameButton , & QPushButton : : clicked , this , & ProfileForm : : onRenameClicked ) ;
connect ( bodyUI - > exportButton , & QPushButton : : clicked , this , & ProfileForm : : onExportClicked ) ;
connect ( bodyUI - > deleteButton , & QPushButton : : clicked , this , & ProfileForm : : onDeleteClicked ) ;
2015-06-04 17:37:54 +08:00
connect ( bodyUI - > logoutButton , & QPushButton : : clicked , this , & ProfileForm : : onLogoutClicked ) ;
2015-06-04 23:09:13 +08:00
connect ( bodyUI - > deletePassButton , & QPushButton : : clicked , this , & ProfileForm : : onDeletePassClicked ) ;
connect ( bodyUI - > changePassButton , & QPushButton : : clicked , this , & ProfileForm : : onChangePassClicked ) ;
2015-07-09 20:06:23 +08:00
connect ( bodyUI - > saveQr , & QPushButton : : clicked , this , & ProfileForm : : onSaveQrClicked ) ;
2015-08-23 23:41:49 +08:00
connect ( bodyUI - > copyQr , & QPushButton : : clicked , this , & ProfileForm : : onCopyQrClicked ) ;
2016-01-24 05:50:57 +08:00
connect ( bodyUI - > toxmeRegisterButton , & QPushButton : : clicked , this , & ProfileForm : : onRegisterButtonClicked ) ;
connect ( bodyUI - > toxmeUpdateButton , & QPushButton : : clicked , this , & ProfileForm : : onRegisterButtonClicked ) ;
2014-10-23 20:41:47 +08:00
2014-11-11 07:20:10 +08:00
connect ( core , & Core : : usernameSet , this , [ = ] ( const QString & val ) { bodyUI - > userName - > setText ( val ) ; } ) ;
connect ( core , & Core : : statusMessageSet , this , [ = ] ( const QString & val ) { bodyUI - > statusMessage - > setText ( val ) ; } ) ;
2015-03-18 02:18:47 +08:00
for ( QComboBox * cb : findChildren < QComboBox * > ( ) )
{
2015-03-16 04:29:23 +08:00
cb - > installEventFilter ( this ) ;
cb - > setFocusPolicy ( Qt : : StrongFocus ) ;
}
2015-06-06 03:37:01 +08:00
retranslateUi ( ) ;
Translator : : registerHandler ( std : : bind ( & ProfileForm : : retranslateUi , this ) , this ) ;
2014-09-15 18:45:59 +08:00
}
2015-07-10 15:43:20 +08:00
void ProfileForm : : prFileLabelUpdate ( )
{
Nexus & nexus = Nexus : : getInstance ( ) ;
2015-08-25 00:44:21 +08:00
bodyUI - > prFileLabel - > setText ( tr ( " Current profile: " ) + nexus . getProfile ( ) - > getName ( ) + " .tox " ) ;
2015-07-10 15:43:20 +08:00
}
2015-03-04 04:29:01 +08:00
ProfileForm : : ~ ProfileForm ( )
2014-09-15 18:45:59 +08:00
{
2015-06-06 03:37:01 +08:00
Translator : : unregister ( this ) ;
2015-05-10 04:35:58 +08:00
delete qr ;
2014-10-28 06:56:21 +08:00
delete bodyUI ;
2015-03-06 02:09:58 +08:00
head - > deleteLater ( ) ;
2015-03-05 03:35:34 +08:00
}
2015-07-01 00:29:09 +08:00
bool ProfileForm : : isShown ( ) const
{
if ( head - > isVisible ( ) )
{
2015-07-20 22:03:01 +08:00
window ( ) - > windowHandle ( ) - > alert ( 0 ) ;
2015-07-01 00:29:09 +08:00
return true ;
}
return false ;
}
2015-06-17 02:25:19 +08:00
void ProfileForm : : show ( ContentLayout * contentLayout )
2015-03-05 03:35:34 +08:00
{
2015-06-17 02:25:19 +08:00
contentLayout - > mainHead - > layout ( ) - > addWidget ( head ) ;
contentLayout - > mainContent - > layout ( ) - > addWidget ( this ) ;
2015-03-05 03:35:34 +08:00
head - > show ( ) ;
QWidget : : show ( ) ;
2015-07-10 15:43:20 +08:00
prFileLabelUpdate ( ) ;
2015-09-29 02:11:45 +08:00
QString DirPath = Settings : : getInstance ( ) . getMakeToxPortable ( ) ? QApplication : : applicationDirPath ( ) :
QDir ( Settings : : getInstance ( ) . getSettingsDirPath ( ) ) . path ( ) . trimmed ( ) ;
2015-07-10 19:42:14 +08:00
bodyUI - > dirPrLink - > setText ( bodyUI - > dirPrLink - > text ( ) . replace ( " Dir_Path " , DirPath ) ) ;
bodyUI - > dirPrLink - > setOpenExternalLinks ( true ) ;
bodyUI - > dirPrLink - > setTextInteractionFlags ( Qt : : LinksAccessibleByMouse | Qt : : TextSelectableByMouse ) ;
2015-09-29 02:11:45 +08:00
bodyUI - > dirPrLink - > setMaximumSize ( bodyUI - > dirPrLink - > sizeHint ( ) ) ;
2015-03-28 04:01:42 +08:00
bodyUI - > userName - > setFocus ( ) ;
2015-03-12 11:00:54 +08:00
bodyUI - > userName - > selectAll ( ) ;
2014-09-15 18:45:59 +08:00
}
2014-09-30 17:44:27 +08:00
2015-10-04 19:51:01 +08:00
bool ProfileForm : : eventFilter ( QObject * object , QEvent * event )
{
if ( object = = static_cast < QObject * > ( profilePicture ) & & event - > type ( ) = = QEvent : : MouseButtonPress )
{
QMouseEvent * mouseEvent = static_cast < QMouseEvent * > ( event ) ;
if ( mouseEvent - > button ( ) = = Qt : : RightButton )
return true ;
}
return false ;
}
void ProfileForm : : showProfilePictureContextMenu ( const QPoint & point )
{
QPoint pos = profilePicture - > mapToGlobal ( point ) ;
QMenu contextMenu ;
2015-10-15 04:31:05 +08:00
QAction * removeAction = contextMenu . addAction ( style ( ) - > standardIcon ( QStyle : : SP_DialogCancelButton ) , tr ( " Remove " ) ) ;
2015-10-04 19:51:01 +08:00
QAction * selectedItem = contextMenu . exec ( pos ) ;
if ( selectedItem = = removeAction )
2015-12-06 02:08:28 +08:00
Nexus : : getProfile ( ) - > removeAvatar ( ) ;
2015-10-04 19:51:01 +08:00
}
2015-03-04 04:29:01 +08:00
void ProfileForm : : copyIdClicked ( )
2014-09-30 17:44:27 +08:00
{
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 ' , " " ) ;
2015-05-18 01:57:55 +08:00
QApplication : : clipboard ( ) - > setText ( txt , QClipboard : : Clipboard ) ;
if ( QApplication : : clipboard ( ) - > supportsSelection ( ) )
QApplication : : clipboard ( ) - > setText ( txt , QClipboard : : Selection ) ;
2014-10-09 16:59:35 +08:00
toxId - > setCursorPosition ( 0 ) ;
2014-11-11 07:20:10 +08:00
2015-01-20 16:07:56 +08:00
if ( ! hasCheck )
{
bodyUI - > toxIdLabel - > setText ( bodyUI - > toxIdLabel - > text ( ) + " ✔ " ) ;
hasCheck = true ;
}
2014-11-11 07:20:10 +08:00
timer . start ( ) ;
2014-09-30 17:44:27 +08:00
}
2015-03-04 04:29:01 +08:00
void ProfileForm : : onUserNameEdited ( )
2014-09-30 17:44:27 +08:00
{
2014-10-06 00:30:31 +08:00
Core : : getInstance ( ) - > setUsername ( bodyUI - > userName - > text ( ) ) ;
2014-09-30 17:44:27 +08:00
}
2015-03-04 04:29:01 +08:00
void ProfileForm : : onStatusMessageEdited ( )
2014-09-30 17:44:27 +08:00
{
2014-10-06 00:30:31 +08:00
Core : : getInstance ( ) - > setStatusMessage ( bodyUI - > statusMessage - > text ( ) ) ;
2014-09-30 17:44:27 +08:00
}
2015-03-04 04:29:01 +08:00
void ProfileForm : : onSelfAvatarLoaded ( const QPixmap & pic )
2014-09-30 17:44:27 +08:00
{
2015-03-04 04:29:01 +08:00
profilePicture - > setPixmap ( pic ) ;
2014-10-06 00:17:01 +08:00
}
2015-03-04 04:29:01 +08:00
void ProfileForm : : setToxId ( const QString & id )
2014-10-06 00:17:01 +08:00
{
2014-10-18 09:15:26 +08:00
toxId - > setText ( id ) ;
toxId - > setCursorPosition ( 0 ) ;
2015-03-17 03:53:35 +08:00
2015-05-10 04:35:58 +08:00
delete qr ;
2015-03-17 03:53:35 +08:00
qr = new QRWidget ( ) ;
qr - > setQRData ( " tox: " + id ) ;
2015-03-15 23:45:28 +08:00
bodyUI - > qrCode - > setPixmap ( QPixmap : : fromImage ( qr - > getImage ( ) - > scaledToWidth ( 150 ) ) ) ;
2014-10-06 00:17:01 +08:00
}
2014-10-09 16:07:26 +08:00
2015-03-04 04:29:01 +08:00
void ProfileForm : : onAvatarClicked ( )
{
2015-05-10 06:56:41 +08:00
auto picToPng = [ ] ( QPixmap pic )
{
QByteArray bytes ;
QBuffer buffer ( & bytes ) ;
buffer . open ( QIODevice : : WriteOnly ) ;
pic . save ( & buffer , " PNG " ) ;
buffer . close ( ) ;
return bytes ;
} ;
2015-08-30 19:25:46 +08:00
QString filename = QFileDialog : : getOpenFileName ( this ,
2015-03-04 04:29:01 +08:00
tr ( " Choose a profile picture " ) ,
QDir : : homePath ( ) ,
Nexus : : getSupportedImageFilter ( ) ) ;
if ( filename . isEmpty ( ) )
return ;
2015-04-24 19:01:50 +08:00
2015-03-04 04:29:01 +08:00
QFile file ( filename ) ;
file . open ( QIODevice : : ReadOnly ) ;
if ( ! file . isOpen ( ) )
{
2015-05-20 05:25:26 +08:00
GUI : : showError ( tr ( " Error " ) , tr ( " Unable to open this file. " ) ) ;
2015-03-04 04:29:01 +08:00
return ;
}
QPixmap pic ;
if ( ! pic . loadFromData ( file . readAll ( ) ) )
{
2015-05-20 05:25:26 +08:00
GUI : : showError ( tr ( " Error " ) , tr ( " Unable to read this image. " ) ) ;
2015-03-04 04:29:01 +08:00
return ;
}
2015-05-10 06:56:41 +08:00
// Limit the avatar size to 64kB
// We do a first rescale to 256x256 in case the image was huge, then keep tryng from here
QByteArray bytes { picToPng ( pic ) } ;
if ( bytes . size ( ) > 65535 )
{
pic = pic . scaled ( 256 , 256 , Qt : : KeepAspectRatio , Qt : : SmoothTransformation ) ;
bytes = picToPng ( pic ) ;
}
if ( bytes . size ( ) > 65535 )
bytes = picToPng ( pic . scaled ( 128 , 128 , Qt : : KeepAspectRatio , Qt : : SmoothTransformation ) ) ;
if ( bytes . size ( ) > 65535 )
bytes = picToPng ( pic . scaled ( 64 , 64 , Qt : : KeepAspectRatio , Qt : : SmoothTransformation ) ) ;
if ( bytes . size ( ) > 65535 )
bytes = picToPng ( pic . scaled ( 32 , 32 , Qt : : KeepAspectRatio , Qt : : SmoothTransformation ) ) ;
// If this happens, you're really doing it on purpose.
if ( bytes . size ( ) > 65535 )
{
2015-08-30 19:25:46 +08:00
QMessageBox : : critical ( this ,
tr ( " Error " ) ,
tr ( " The supplied image is too large. \n Please use another image. " ) ) ;
2015-05-10 06:56:41 +08:00
return ;
}
2015-03-04 04:29:01 +08:00
2015-04-20 05:12:44 +08:00
Nexus : : getCore ( ) - > setAvatar ( bytes ) ;
2015-03-04 04:29:01 +08:00
}
void ProfileForm : : onRenameClicked ( )
2014-10-09 16:07:26 +08:00
{
2015-06-04 19:01:30 +08:00
Nexus & nexus = Nexus : : getInstance ( ) ;
QString cur = nexus . getProfile ( ) - > getName ( ) ;
2014-10-09 16:07:26 +08:00
QString title = tr ( " Rename \" %1 \" " , " renaming a profile " ) . arg ( cur ) ;
2014-10-20 11:36:39 +08:00
do
2014-10-09 16:07:26 +08:00
{
2014-10-20 11:36:39 +08:00
QString name = QInputDialog : : getText ( this , title , title + " : " ) ;
if ( name . isEmpty ( ) ) break ;
2014-10-09 16:07:26 +08:00
name = Core : : sanitize ( name ) ;
2015-06-04 19:01:30 +08:00
2015-06-05 18:44:22 +08:00
if ( Profile : : exists ( name ) )
2015-06-04 21:27:22 +08:00
GUI : : showError ( tr ( " Profile already exists " , " rename failure title " ) ,
tr ( " A profile named \" %1 \" already exists. " , " rename confirm text " ) . arg ( name ) ) ;
else if ( ! nexus . getProfile ( ) - > rename ( name ) )
GUI : : showError ( tr ( " Failed to rename " , " rename failed title " ) ,
tr ( " Couldn't rename the profile to \" %1 \" " ) . arg ( cur ) ) ;
else
2015-07-10 15:43:20 +08:00
{
prFileLabelUpdate ( ) ;
2014-10-20 11:36:39 +08:00
break ;
2015-07-10 15:43:20 +08:00
}
2014-10-20 11:36:39 +08:00
} while ( true ) ;
2014-10-09 16:07:26 +08:00
}
2015-03-04 04:29:01 +08:00
void ProfileForm : : onExportClicked ( )
2014-10-09 16:07:26 +08:00
{
2015-06-04 17:37:54 +08:00
QString current = Nexus : : getProfile ( ) - > getName ( ) + Core : : TOX_EXT ;
2015-08-30 19:25:46 +08:00
QString path = QFileDialog : : getSaveFileName ( this ,
tr ( " Export profile " , " save dialog title " ) ,
2015-03-17 05:15:34 +08:00
QDir : : home ( ) . filePath ( current ) ,
2014-10-09 16:07:26 +08:00
tr ( " Tox save file (*.tox) " , " save dialog filter " ) ) ;
2014-10-14 05:17:42 +08:00
if ( ! path . isEmpty ( ) )
2014-11-12 06:06:15 +08:00
{
2015-04-26 23:39:39 +08:00
if ( ! Nexus : : tryRemoveFile ( path ) )
2014-11-12 06:06:15 +08:00
{
2015-03-17 05:41:59 +08:00
GUI : : showWarning ( tr ( " Location not writable " , " Title of permissions popup " ) , tr ( " You do not have permission to write that location. Choose another, or cancel the save dialog. " , " text of permissions popup " ) ) ;
return ;
2014-11-12 06:06:15 +08:00
}
2015-06-05 22:24:47 +08:00
if ( ! QFile : : copy ( Settings : : getInstance ( ) . getSettingsDirPath ( ) + current , path ) )
2015-03-17 05:41:59 +08:00
GUI : : showWarning ( tr ( " Failed to copy file " ) , tr ( " The file you chose could not be written to. " ) ) ;
2014-11-12 06:06:15 +08:00
}
2014-10-09 16:07:26 +08:00
}
2015-03-04 04:29:01 +08:00
void ProfileForm : : onDeleteClicked ( )
2014-10-09 16:07:26 +08:00
{
2015-08-30 19:25:46 +08:00
if ( GUI : : askQuestion (
tr ( " Really delete profile? " , " deletion confirmation title " ) ,
tr ( " Are you sure you want to delete this profile? " , " deletion confirmation text " ) ) )
2014-10-09 16:07:26 +08:00
{
2015-06-04 18:43:28 +08:00
Nexus & nexus = Nexus : : getInstance ( ) ;
2016-04-29 13:38:15 +08:00
2016-04-30 05:10:21 +08:00
QVector < QString > manualDeleteFiles = nexus . getProfile ( ) - > remove ( ) ;
if ( ! manualDeleteFiles . empty ( ) )
2016-04-29 13:38:15 +08:00
{
2016-04-30 05:10:21 +08:00
QString message = tr ( " The following files could not be deleted: " , " deletion failed text part 1 " ) + " \n \n " ;
for ( auto & file : manualDeleteFiles )
{
message = message + file + " \n " ;
}
message = message + " \n " + tr ( " Please manually remove them. " , " deletion failed text part 2 " ) ;
GUI : : showError ( tr ( " Files could not be deleted! " , " deletion failed title " ) , message ) ;
2016-04-29 13:38:15 +08:00
}
2015-06-04 18:43:28 +08:00
nexus . showLogin ( ) ;
2014-10-09 16:07:26 +08:00
}
}
2015-06-04 17:37:54 +08:00
void ProfileForm : : onLogoutClicked ( )
2014-10-09 16:07:26 +08:00
{
2015-06-04 17:56:19 +08:00
Nexus & nexus = Nexus : : getInstance ( ) ;
2015-06-06 21:54:58 +08:00
Settings : : getInstance ( ) . saveGlobal ( ) ;
2015-06-04 17:56:19 +08:00
nexus . showLogin ( ) ;
2014-10-09 16:07:26 +08:00
}
2014-10-20 11:07:51 +08:00
2015-06-04 17:37:54 +08:00
void ProfileForm : : onCopyQrClicked ( )
2015-03-15 23:45:28 +08:00
{
2015-03-16 02:53:10 +08:00
QApplication : : clipboard ( ) - > setImage ( * qr - > getImage ( ) ) ;
2015-03-15 23:45:28 +08:00
}
2015-06-04 17:37:54 +08:00
void ProfileForm : : onSaveQrClicked ( )
2015-03-15 23:45:28 +08:00
{
2015-06-04 17:37:54 +08:00
QString current = Nexus : : getProfile ( ) - > getName ( ) + " .png " ;
2015-08-30 19:25:46 +08:00
QString path = QFileDialog : : getSaveFileName ( this ,
tr ( " Save " , " save qr image " ) ,
2015-03-17 05:41:59 +08:00
QDir : : home ( ) . filePath ( current ) ,
2015-03-15 23:45:28 +08:00
tr ( " Save QrCode (*.png) " , " save dialog filter " ) ) ;
if ( ! path . isEmpty ( ) )
{
2015-04-26 23:39:39 +08:00
if ( ! Nexus : : tryRemoveFile ( path ) )
2015-03-15 23:45:28 +08:00
{
2015-03-17 05:41:59 +08:00
GUI : : showWarning ( tr ( " Location not writable " , " Title of permissions popup " ) , tr ( " You do not have permission to write that location. Choose another, or cancel the save dialog. " , " text of permissions popup " ) ) ;
return ;
2015-03-15 23:45:28 +08:00
}
2015-03-17 05:41:59 +08:00
if ( ! qr - > saveImage ( path ) )
GUI : : showWarning ( tr ( " Failed to copy file " ) , tr ( " The file you chose could not be written to. " ) ) ;
2015-03-15 23:45:28 +08:00
}
}
2015-06-04 23:09:13 +08:00
void ProfileForm : : onDeletePassClicked ( )
{
Profile * pro = Nexus : : getProfile ( ) ;
if ( ! pro - > isEncrypted ( ) )
{
GUI : : showInfo ( tr ( " Nothing to remove " ) , tr ( " Your profile does not have a password! " ) ) ;
return ;
}
if ( ! GUI : : askQuestion ( tr ( " Really delete password? " , " deletion confirmation title " ) ,
tr ( " Are you sure you want to delete your password? " , " deletion confirmation text " ) ) )
return ;
Nexus : : getProfile ( ) - > setPassword ( QString ( ) ) ;
}
void ProfileForm : : onChangePassClicked ( )
{
2015-06-04 23:45:20 +08:00
SetPasswordDialog * dialog = new SetPasswordDialog ( tr ( " Please enter a new password. " ) , QString ( ) , 0 ) ;
int r = dialog - > exec ( ) ;
if ( r = = QDialog : : Rejected )
return ;
2015-06-04 23:09:13 +08:00
2015-06-04 23:45:20 +08:00
QString newPass = dialog - > getPassword ( ) ;
Nexus : : getProfile ( ) - > setPassword ( newPass ) ;
2015-06-04 23:09:13 +08:00
}
2015-06-06 03:37:01 +08:00
void ProfileForm : : retranslateUi ( )
{
bodyUI - > retranslateUi ( this ) ;
2015-08-30 19:25:46 +08:00
nameLabel - > setText ( tr ( " User Profile " ) ) ;
2015-06-06 03:37:01 +08:00
// We have to add the toxId tooltip here and not in the .ui or Qt won't know how to translate it dynamically
toxId - > setToolTip ( tr ( " This bunch of characters tells other Tox clients how to contact you. \n Share it with your friends to communicate. " ) ) ;
}
2015-12-30 23:45:46 +08:00
2016-01-24 05:50:57 +08:00
void ProfileForm : : showRegisterToxme ( )
{
bodyUI - > toxmeUsername - > setText ( " " ) ;
bodyUI - > toxmeBio - > setText ( " " ) ;
bodyUI - > toxmePrivacy - > setChecked ( false ) ;
bodyUI - > toxmeRegisterButton - > show ( ) ;
bodyUI - > toxmeUpdateButton - > hide ( ) ;
bodyUI - > toxmePassword - > hide ( ) ;
bodyUI - > toxmePasswordLabel - > hide ( ) ;
}
2016-04-13 06:20:16 +08:00
void ProfileForm : : showExistingToxme ( )
2016-01-24 05:50:57 +08:00
{
QStringList info = Settings : : getInstance ( ) . getToxmeInfo ( ) . split ( " @ " ) ;
bodyUI - > toxmeUsername - > setText ( info [ 0 ] ) ;
bodyUI - > toxmeServersList - > addItem ( info [ 1 ] ) ;
QString bio = Settings : : getInstance ( ) . getToxmeBio ( ) ;
bodyUI - > toxmeBio - > setText ( bio ) ;
bool priv = Settings : : getInstance ( ) . getToxmePriv ( ) ;
bodyUI - > toxmePrivacy - > setChecked ( priv ) ;
QString pass = Settings : : getInstance ( ) . getToxmePass ( ) ;
bodyUI - > toxmePassword - > setText ( pass ) ;
bodyUI - > toxmePassword - > show ( ) ;
bodyUI - > toxmePasswordLabel - > show ( ) ;
bodyUI - > toxmeRegisterButton - > hide ( ) ;
bodyUI - > toxmeUpdateButton - > show ( ) ;
}
2015-12-30 23:45:46 +08:00
void ProfileForm : : onRegisterButtonClicked ( )
{
QString name = bodyUI - > toxmeUsername - > text ( ) ;
if ( name . isEmpty ( ) )
return ;
bodyUI - > toxmeRegisterButton - > setEnabled ( false ) ;
bodyUI - > toxmeUpdateButton - > setEnabled ( false ) ;
bodyUI - > toxmeRegisterButton - > setText ( tr ( " Register (processing) " ) ) ;
bodyUI - > toxmeUpdateButton - > setText ( tr ( " Update (processing) " ) ) ;
QString id = toxId - > text ( ) ;
QString bio = bodyUI - > toxmeBio - > text ( ) ;
QString server = bodyUI - > toxmeServersList - > currentText ( ) ;
bool privacy = bodyUI - > toxmePrivacy - > isChecked ( ) ;
2016-04-12 06:59:03 +08:00
Core * oldCore = Core : : getInstance ( ) ;
2015-12-30 23:45:46 +08:00
Toxme : : ExecCode code = Toxme : : ExecCode : : Ok ;
QString response = Toxme : : createAddress ( code , server , id , name , privacy , bio ) ;
2016-04-12 06:59:03 +08:00
Core * newCore = Core : : getInstance ( ) ;
2016-04-13 06:20:16 +08:00
// Make sure the user didn't logout (or logout and login)
// before the request is finished, else qTox will crash.
2016-04-12 06:59:03 +08:00
if ( oldCore = = newCore )
2016-04-08 07:06:39 +08:00
{
switch ( code ) {
case Toxme : : Updated :
2016-04-12 06:59:03 +08:00
GUI : : showInfo ( tr ( " Done! " ) , tr ( " Account %1@%2 updated successfully " ) . arg ( name , server ) ) ;
2016-04-08 07:06:39 +08:00
Settings : : getInstance ( ) . setToxme ( name , server , bio , privacy ) ;
2016-04-13 06:20:16 +08:00
showExistingToxme ( ) ;
2016-04-08 07:06:39 +08:00
break ;
case Toxme : : Ok :
2016-04-12 06:59:03 +08:00
GUI : : showInfo ( tr ( " Done! " ) , tr ( " Successfully added %1@%2 to the database. Save your password " ) . arg ( name , server ) ) ;
2016-04-08 07:06:39 +08:00
Settings : : getInstance ( ) . setToxme ( name , server , bio , privacy , response ) ;
2016-04-13 06:20:16 +08:00
showExistingToxme ( ) ;
2016-04-08 07:06:39 +08:00
break ;
default :
QString errorMessage = Toxme : : getErrorMessage ( code ) ;
2016-04-12 06:59:03 +08:00
GUI : : showWarning ( tr ( " Toxme error " ) , errorMessage ) ;
2016-04-08 07:06:39 +08:00
}
2016-01-24 05:50:57 +08:00
2016-04-08 07:06:39 +08:00
bodyUI - > toxmeRegisterButton - > setEnabled ( true ) ;
bodyUI - > toxmeUpdateButton - > setEnabled ( true ) ;
bodyUI - > toxmeRegisterButton - > setText ( tr ( " Register " ) ) ;
bodyUI - > toxmeUpdateButton - > setText ( tr ( " Update " ) ) ;
}
2015-12-30 23:45:46 +08:00
}