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 "widget.h"
2014-08-11 16:00:08 +08:00
# include "ui_mainwindow.h"
2014-10-08 12:26:25 +08:00
# include "src/core.h"
# include "src/misc/settings.h"
# include "src/friend.h"
# include "src/friendlist.h"
# include "tool/friendrequestdialog.h"
# include "friendwidget.h"
# include "src/grouplist.h"
# include "src/group.h"
# include "groupwidget.h"
# include "form/groupchatform.h"
# include "src/misc/style.h"
# include "friendlistwidget.h"
2014-10-24 04:07:44 +08:00
# include "src/video/camera.h"
2014-10-08 12:26:25 +08:00
# include "form/chatform.h"
# include "maskablepixmapwidget.h"
2014-11-10 00:55:38 +08:00
# include "src/historykeeper.h"
# include "form/inputpassworddialog.h"
# include "src/autoupdate.h"
2014-06-25 04:11:11 +08:00
# include <QMessageBox>
# include <QDebug>
2014-07-01 10:12:17 +08:00
# include <QFile>
# include <QString>
2014-09-24 22:51:16 +08:00
# include <QBuffer>
2014-07-01 10:12:17 +08:00
# include <QPainter>
# include <QMouseEvent>
2014-07-02 05:50:07 +08:00
# include <QClipboard>
2014-09-11 21:44:34 +08:00
# include <QThread>
2014-09-24 22:51:16 +08:00
# include <QFileDialog>
2014-10-09 13:30:33 +08:00
# include <QInputDialog>
2014-10-16 17:47:58 +08:00
# include <QTimer>
2014-10-09 06:31:20 +08:00
# include <QStyleFactory>
2014-10-18 07:06:30 +08:00
# include <QTranslator>
# include <tox/tox.h>
2014-06-25 04:11:11 +08:00
Widget * Widget : : instance { nullptr } ;
2014-08-11 16:00:08 +08:00
Widget : : Widget ( QWidget * parent )
: QMainWindow ( parent ) ,
ui ( new Ui : : MainWindow ) ,
2014-09-02 00:20:28 +08:00
activeChatroomWidget { nullptr }
2014-10-18 22:54:52 +08:00
{
2014-10-18 07:06:30 +08:00
translator = new QTranslator ;
setTranslation ( ) ;
2014-10-15 23:35:58 +08:00
}
void Widget : : init ( )
2014-06-25 04:11:11 +08:00
{
ui - > setupUi ( this ) ;
2014-10-21 02:03:44 +08:00
2014-11-09 06:14:22 +08:00
if ( QSystemTrayIcon : : isSystemTrayAvailable ( ) )
2014-10-21 02:03:44 +08:00
{
icon = new QSystemTrayIcon ( this ) ;
icon - > setIcon ( this - > windowIcon ( ) ) ;
trayMenu = new QMenu ;
2014-11-09 00:47:13 +08:00
statusOnline = new QAction ( tr ( " Online " ) , this ) ;
2014-10-21 02:03:44 +08:00
statusOnline - > setIcon ( QIcon ( " :ui/statusButton/dot_online.png " ) ) ;
connect ( statusOnline , SIGNAL ( triggered ( ) ) , this , SLOT ( setStatusOnline ( ) ) ) ;
2014-11-09 00:47:13 +08:00
statusAway = new QAction ( tr ( " Away " ) , this ) ;
2014-10-21 02:03:44 +08:00
statusAway - > setIcon ( QIcon ( " :ui/statusButton/dot_idle.png " ) ) ;
connect ( statusAway , SIGNAL ( triggered ( ) ) , this , SLOT ( setStatusAway ( ) ) ) ;
2014-11-09 00:47:13 +08:00
statusBusy = new QAction ( tr ( " Busy " ) , this ) ;
2014-10-21 02:03:44 +08:00
connect ( statusBusy , SIGNAL ( triggered ( ) ) , this , SLOT ( setStatusBusy ( ) ) ) ;
statusBusy - > setIcon ( QIcon ( " :ui/statusButton/dot_busy.png " ) ) ;
actionQuit = new QAction ( tr ( " &Quit " ) , this ) ;
connect ( actionQuit , SIGNAL ( triggered ( ) ) , qApp , SLOT ( quit ( ) ) ) ;
trayMenu - > addAction ( new QAction ( tr ( " Change status to: " ) , this ) ) ;
trayMenu - > addAction ( statusOnline ) ;
trayMenu - > addAction ( statusAway ) ;
trayMenu - > addAction ( statusBusy ) ;
trayMenu - > addSeparator ( ) ;
trayMenu - > addAction ( actionQuit ) ;
icon - > setContextMenu ( trayMenu ) ;
connect ( icon ,
SIGNAL ( activated ( QSystemTrayIcon : : ActivationReason ) ) ,
this ,
SLOT ( onIconClick ( QSystemTrayIcon : : ActivationReason ) ) ) ;
icon - > show ( ) ;
if ( Settings : : getInstance ( ) . getAutostartInTray ( ) = = false )
this - > show ( ) ;
}
else
{
qWarning ( ) < < " No system tray detected! " ;
this - > show ( ) ;
}
2014-07-01 10:12:17 +08:00
2014-08-11 16:00:08 +08:00
ui - > statusbar - > hide ( ) ;
ui - > menubar - > hide ( ) ;
//restore window state
restoreGeometry ( Settings : : getInstance ( ) . getWindowGeometry ( ) ) ;
restoreState ( Settings : : getInstance ( ) . getWindowState ( ) ) ;
2014-08-11 20:07:27 +08:00
ui - > mainSplitter - > restoreState ( Settings : : getInstance ( ) . getSplitterState ( ) ) ;
2014-08-11 16:00:08 +08:00
2014-10-04 16:54:50 +08:00
layout ( ) - > setContentsMargins ( 0 , 0 , 0 , 0 ) ;
ui - > friendList - > setStyleSheet ( Style : : getStylesheet ( " :ui/friendList/friendList.css " ) ) ;
2014-07-01 10:12:17 +08:00
2014-10-18 22:54:52 +08:00
profilePicture = new MaskablePixmapWidget ( this , QSize ( 40 , 40 ) , " :/img/avatar_mask.png " ) ;
2014-09-27 23:58:56 +08:00
profilePicture - > setPixmap ( QPixmap ( " :/img/contact_dark.png " ) ) ;
2014-09-28 01:13:46 +08:00
profilePicture - > setClickable ( true ) ;
2014-11-03 07:26:23 +08:00
ui - > myProfile - > insertWidget ( 0 , profilePicture ) ;
ui - > myProfile - > insertSpacing ( 1 , 7 ) ;
2014-09-26 22:39:29 +08:00
2014-06-25 04:11:11 +08:00
ui - > mainContent - > setLayout ( new QVBoxLayout ( ) ) ;
ui - > mainHead - > setLayout ( new QVBoxLayout ( ) ) ;
2014-06-26 04:43:28 +08:00
ui - > mainHead - > layout ( ) - > setMargin ( 0 ) ;
ui - > mainHead - > layout ( ) - > setSpacing ( 0 ) ;
2014-10-09 06:31:20 +08:00
if ( QStyleFactory : : keys ( ) . contains ( Settings : : getInstance ( ) . getStyle ( ) )
& & Settings : : getInstance ( ) . getStyle ( ) ! = " None " )
{
ui - > mainHead - > setStyle ( QStyleFactory : : create ( Settings : : getInstance ( ) . getStyle ( ) ) ) ;
ui - > mainContent - > setStyle ( QStyleFactory : : create ( Settings : : getInstance ( ) . getStyle ( ) ) ) ;
}
ui - > mainHead - > setStyleSheet ( Style : : getStylesheet ( " :ui/settings/mainHead.css " ) ) ;
2014-10-07 17:50:36 +08:00
ui - > mainContent - > setStyleSheet ( Style : : getStylesheet ( " :ui/settings/mainContent.css " ) ) ;
2014-11-03 07:26:23 +08:00
ui - > statusHead - > setStyleSheet ( Style : : getStylesheet ( " :/ui/window/statusPanel.css " ) ) ;
2014-08-11 16:00:08 +08:00
2014-08-22 23:19:16 +08:00
contactListWidget = new FriendListWidget ( ) ;
ui - > friendList - > setWidget ( contactListWidget ) ;
2014-08-12 01:46:14 +08:00
ui - > friendList - > setLayoutDirection ( Qt : : RightToLeft ) ;
2014-06-25 04:11:11 +08:00
2014-09-01 17:08:02 +08:00
ui - > nameLabel - > setEditable ( true ) ;
ui - > statusLabel - > setEditable ( true ) ;
2014-10-04 16:24:20 +08:00
ui - > statusPanel - > setStyleSheet ( Style : : getStylesheet ( " :/ui/window/statusPanel.css " ) ) ;
2014-07-06 03:30:00 +08:00
2014-07-06 04:54:27 +08:00
QMenu * statusButtonMenu = new QMenu ( ui - > statusButton ) ;
2014-08-29 17:56:42 +08:00
QAction * setStatusOnline = statusButtonMenu - > addAction ( Widget : : tr ( " Online " , " Button to set your status to 'Online' " ) ) ;
2014-07-06 04:54:27 +08:00
setStatusOnline - > setIcon ( QIcon ( " :ui/statusButton/dot_online.png " ) ) ;
2014-08-29 17:56:42 +08:00
QAction * setStatusAway = statusButtonMenu - > addAction ( Widget : : tr ( " Away " , " Button to set your status to 'Away' " ) ) ;
2014-07-06 04:54:27 +08:00
setStatusAway - > setIcon ( QIcon ( " :ui/statusButton/dot_idle.png " ) ) ;
2014-08-29 17:56:42 +08:00
QAction * setStatusBusy = statusButtonMenu - > addAction ( Widget : : tr ( " Busy " , " Button to set your status to 'Busy' " ) ) ;
2014-07-06 04:54:27 +08:00
setStatusBusy - > setIcon ( QIcon ( " :ui/statusButton/dot_busy.png " ) ) ;
ui - > statusButton - > setMenu ( statusButtonMenu ) ;
2014-08-11 19:56:52 +08:00
// disable proportional scaling
2014-08-11 20:07:27 +08:00
ui - > mainSplitter - > setStretchFactor ( 0 , 0 ) ;
ui - > mainSplitter - > setStretchFactor ( 1 , 1 ) ;
2014-06-25 04:11:11 +08:00
2014-10-04 16:24:20 +08:00
ui - > statusButton - > setProperty ( " status " , " offline " ) ;
Style : : repolish ( ui - > statusButton ) ;
2014-07-06 21:51:24 +08:00
2014-09-03 06:01:04 +08:00
// Disable some widgets until we're connected to the DHT
ui - > statusButton - > setEnabled ( false ) ;
2014-10-16 17:47:58 +08:00
idleTimer = new QTimer ( ) ;
int mins = Settings : : getInstance ( ) . getAutoAwayTime ( ) ;
if ( mins > 0 )
idleTimer - > start ( mins * 1000 * 60 ) ;
2014-06-25 04:11:11 +08:00
qRegisterMetaType < Status > ( " Status " ) ;
2014-07-12 16:54:52 +08:00
qRegisterMetaType < vpx_image > ( " vpx_image " ) ;
2014-06-25 04:11:11 +08:00
qRegisterMetaType < uint8_t > ( " uint8_t " ) ;
2014-10-03 07:53:44 +08:00
qRegisterMetaType < uint16_t > ( " uint16_t " ) ;
2014-06-26 04:43:28 +08:00
qRegisterMetaType < int32_t > ( " int32_t " ) ;
2014-07-13 00:40:01 +08:00
qRegisterMetaType < int64_t > ( " int64_t " ) ;
2014-09-24 23:28:38 +08:00
qRegisterMetaType < QPixmap > ( " QPixmap " ) ;
2014-06-27 01:45:47 +08:00
qRegisterMetaType < ToxFile > ( " ToxFile " ) ;
qRegisterMetaType < ToxFile : : FileDirection > ( " ToxFile::FileDirection " ) ;
2014-10-22 21:36:36 +08:00
qRegisterMetaType < Core : : PasswordType > ( " Core::PasswordType " ) ;
2014-06-25 04:11:11 +08:00
2014-10-09 13:30:33 +08:00
QString profilePath = detectProfile ( ) ;
2014-06-25 04:11:11 +08:00
coreThread = new QThread ( this ) ;
2014-10-11 06:43:15 +08:00
core = new Core ( Camera : : getInstance ( ) , coreThread , profilePath ) ;
2014-06-25 04:11:11 +08:00
core - > moveToThread ( coreThread ) ;
connect ( coreThread , & QThread : : started , core , & Core : : start ) ;
2014-10-18 22:54:52 +08:00
filesForm = new FilesForm ( ) ;
addFriendForm = new AddFriendForm ;
settingsWidget = new SettingsWidget ( ) ;
2014-06-25 04:11:11 +08:00
connect ( core , & Core : : connected , this , & Widget : : onConnected ) ;
connect ( core , & Core : : disconnected , this , & Widget : : onDisconnected ) ;
connect ( core , & Core : : failedToStart , this , & Widget : : onFailedToStartCore ) ;
2014-10-06 04:49:44 +08:00
connect ( core , & Core : : badProxy , this , & Widget : : onBadProxyCore ) ;
2014-06-25 04:11:11 +08:00
connect ( core , & Core : : statusSet , this , & Widget : : onStatusSet ) ;
connect ( core , & Core : : usernameSet , this , & Widget : : setUsername ) ;
connect ( core , & Core : : statusMessageSet , this , & Widget : : setStatusMessage ) ;
2014-09-24 22:51:16 +08:00
connect ( core , & Core : : selfAvatarChanged , this , & Widget : : onSelfAvatarLoaded ) ;
2014-10-18 22:54:52 +08:00
connect ( core , SIGNAL ( fileDownloadFinished ( const QString & ) ) , filesForm , SLOT ( onFileDownloadComplete ( const QString & ) ) ) ;
connect ( core , SIGNAL ( fileUploadFinished ( const QString & ) ) , filesForm , SLOT ( onFileUploadComplete ( const QString & ) ) ) ;
2014-06-25 04:11:11 +08:00
connect ( core , & Core : : friendAdded , this , & Widget : : addFriend ) ;
2014-06-27 01:45:47 +08:00
connect ( core , & Core : : failedToAddFriend , this , & Widget : : addFriendFailed ) ;
2014-06-25 04:11:11 +08:00
connect ( core , & Core : : friendUsernameChanged , this , & Widget : : onFriendUsernameChanged ) ;
connect ( core , & Core : : friendStatusChanged , this , & Widget : : onFriendStatusChanged ) ;
connect ( core , & Core : : friendStatusMessageChanged , this , & Widget : : onFriendStatusMessageChanged ) ;
connect ( core , & Core : : friendRequestReceived , this , & Widget : : onFriendRequestReceived ) ;
connect ( core , & Core : : friendMessageReceived , this , & Widget : : onFriendMessageReceived ) ;
connect ( core , & Core : : groupInviteReceived , this , & Widget : : onGroupInviteReceived ) ;
connect ( core , & Core : : groupMessageReceived , this , & Widget : : onGroupMessageReceived ) ;
connect ( core , & Core : : groupNamelistChanged , this , & Widget : : onGroupNamelistChanged ) ;
2014-06-30 05:38:48 +08:00
connect ( core , & Core : : emptyGroupCreated , this , & Widget : : onEmptyGroupCreated ) ;
2014-10-08 10:20:38 +08:00
connect ( core , & Core : : avInvite , this , & Widget : : playRingtone ) ;
2014-10-15 10:06:44 +08:00
connect ( core , & Core : : blockingClearContacts , this , & Widget : : clearContactsList , Qt : : BlockingQueuedConnection ) ;
2014-10-16 00:18:32 +08:00
connect ( core , & Core : : blockingGetPassword , this , & Widget : : getPassword , Qt : : BlockingQueuedConnection ) ;
2014-06-25 04:11:11 +08:00
2014-09-24 00:35:06 +08:00
connect ( core , SIGNAL ( messageSentResult ( int , QString , int ) ) , this , SLOT ( onMessageSendResult ( int , QString , int ) ) ) ;
connect ( core , SIGNAL ( groupSentResult ( int , QString , int ) ) , this , SLOT ( onGroupSendResult ( int , QString , int ) ) ) ;
2014-06-25 04:11:11 +08:00
connect ( this , & Widget : : statusSet , core , & Core : : setStatus ) ;
connect ( this , & Widget : : friendRequested , core , & Core : : requestFriendship ) ;
connect ( this , & Widget : : friendRequestAccepted , core , & Core : : acceptFriendRequest ) ;
2014-10-15 10:06:44 +08:00
connect ( this , & Widget : : changeProfile , core , & Core : : switchConfiguration ) ;
2014-06-25 04:11:11 +08:00
connect ( ui - > addButton , SIGNAL ( clicked ( ) ) , this , SLOT ( onAddClicked ( ) ) ) ;
connect ( ui - > groupButton , SIGNAL ( clicked ( ) ) , this , SLOT ( onGroupClicked ( ) ) ) ;
connect ( ui - > transferButton , SIGNAL ( clicked ( ) ) , this , SLOT ( onTransferClicked ( ) ) ) ;
connect ( ui - > settingsButton , SIGNAL ( clicked ( ) ) , this , SLOT ( onSettingsClicked ( ) ) ) ;
2014-10-18 22:54:52 +08:00
connect ( ui - > nameLabel , SIGNAL ( textChanged ( QString , QString ) ) , this , SLOT ( onUsernameChanged ( QString , QString ) ) ) ;
connect ( ui - > statusLabel , SIGNAL ( textChanged ( QString , QString ) ) , this , SLOT ( onStatusMessageChanged ( QString , QString ) ) ) ;
2014-09-26 22:39:29 +08:00
connect ( profilePicture , SIGNAL ( clicked ( ) ) , this , SLOT ( onAvatarClicked ( ) ) ) ;
2014-07-06 04:54:27 +08:00
connect ( setStatusOnline , SIGNAL ( triggered ( ) ) , this , SLOT ( setStatusOnline ( ) ) ) ;
connect ( setStatusAway , SIGNAL ( triggered ( ) ) , this , SLOT ( setStatusAway ( ) ) ) ;
connect ( setStatusBusy , SIGNAL ( triggered ( ) ) , this , SLOT ( setStatusBusy ( ) ) ) ;
2014-10-18 22:54:52 +08:00
connect ( addFriendForm , SIGNAL ( friendRequested ( QString , QString ) ) , this , SIGNAL ( friendRequested ( QString , QString ) ) ) ;
2014-10-16 17:47:58 +08:00
connect ( idleTimer , & QTimer : : timeout , this , & Widget : : onUserAway ) ;
2014-06-25 04:11:11 +08:00
coreThread - > start ( ) ;
2014-10-18 22:54:52 +08:00
addFriendForm - > show ( * ui ) ;
2014-11-10 07:31:29 +08:00
# if (AUTOUPDATE_ENABLED)
if ( Settings : : getInstance ( ) . getCheckUpdates ( ) )
AutoUpdater : : checkUpdatesAsyncInteractive ( ) ;
# endif
2014-10-18 22:54:52 +08:00
}
void Widget : : setTranslation ( )
{
// Load translations
QCoreApplication : : removeTranslator ( translator ) ;
QString locale ;
if ( ( locale = Settings : : getInstance ( ) . getTranslation ( ) ) . isEmpty ( ) )
locale = QLocale : : system ( ) . name ( ) . section ( ' _ ' , 0 , 0 ) ;
2014-11-02 21:24:06 +08:00
if ( locale = = " en " )
return ;
2014-10-18 22:54:52 +08:00
if ( translator - > load ( locale , " :translations/ " ) )
qDebug ( ) < < " Loaded translation " < < locale ;
else
qDebug ( ) < < " Error loading translation " < < locale ;
QCoreApplication : : installTranslator ( translator ) ;
2014-06-25 04:11:11 +08:00
}
Widget : : ~ Widget ( )
{
2014-07-04 01:38:30 +08:00
core - > saveConfiguration ( ) ;
2014-06-25 04:11:11 +08:00
coreThread - > exit ( ) ;
2014-08-28 19:09:26 +08:00
coreThread - > wait ( 500 ) ; // In case of deadlock (can happen with QtAudio/PA bugs)
if ( ! coreThread - > isFinished ( ) )
coreThread - > terminate ( ) ;
2014-06-25 04:11:11 +08:00
delete core ;
2014-09-30 17:44:27 +08:00
delete settingsWidget ;
2014-10-18 22:54:52 +08:00
delete addFriendForm ;
delete filesForm ;
2014-06-25 04:11:11 +08:00
2014-11-07 22:55:32 +08:00
FriendList : : clear ( ) ;
2014-06-25 04:11:11 +08:00
for ( Group * g : GroupList : : groupList )
delete g ;
GroupList : : groupList . clear ( ) ;
2014-10-21 02:03:44 +08:00
delete trayMenu ;
delete icon ;
2014-06-25 04:11:11 +08:00
delete ui ;
2014-10-28 06:56:21 +08:00
delete translator ;
2014-09-30 17:44:27 +08:00
instance = nullptr ;
2014-06-25 04:11:11 +08:00
}
Widget * Widget : : getInstance ( )
{
2014-10-21 02:03:44 +08:00
2014-06-25 04:11:11 +08:00
if ( ! instance )
2014-10-15 23:35:58 +08:00
{
2014-06-25 04:11:11 +08:00
instance = new Widget ( ) ;
2014-10-15 23:35:58 +08:00
instance - > init ( ) ;
}
2014-06-25 04:11:11 +08:00
return instance ;
}
2014-07-07 06:03:37 +08:00
QThread * Widget : : getCoreThread ( )
{
return coreThread ;
}
2014-08-11 16:00:08 +08:00
void Widget : : closeEvent ( QCloseEvent * event )
2014-07-03 14:43:23 +08:00
{
2014-10-20 03:47:06 +08:00
if ( Settings : : getInstance ( ) . getCloseToTray ( ) = = true )
{
event - > ignore ( ) ;
this - > hide ( ) ;
}
else
{
Settings : : getInstance ( ) . setWindowGeometry ( saveGeometry ( ) ) ;
Settings : : getInstance ( ) . setWindowState ( saveState ( ) ) ;
Settings : : getInstance ( ) . setSplitterState ( ui - > mainSplitter - > saveState ( ) ) ;
QWidget : : closeEvent ( event ) ;
}
2014-07-03 14:43:23 +08:00
}
2014-07-01 10:12:17 +08:00
2014-10-20 19:50:12 +08:00
void Widget : : changeEvent ( QEvent * event )
{
if ( event - > type ( ) = = QEvent : : WindowStateChange )
{
2014-11-10 17:22:20 +08:00
if ( isMinimized ( ) & & Settings : : getInstance ( ) . getMinimizeToTray ( ) )
2014-10-20 19:50:12 +08:00
{
this - > hide ( ) ;
}
}
2014-07-03 14:43:23 +08:00
}
2014-07-01 10:12:17 +08:00
2014-10-09 13:30:33 +08:00
QString Widget : : detectProfile ( )
{
QDir dir ( Settings : : getSettingsDirPath ( ) ) ;
QString path , profile = Settings : : getInstance ( ) . getCurrentProfile ( ) ;
path = dir . filePath ( profile + Core : : TOX_EXT ) ;
QFile file ( path ) ;
if ( profile = = " " | | ! file . exists ( ) )
{
2014-10-09 17:26:32 +08:00
Settings : : getInstance ( ) . setCurrentProfile ( " " ) ;
2014-10-09 13:30:33 +08:00
# if 1 // deprecation attempt
// if the last profile doesn't exist, fall back to old "data"
path = dir . filePath ( Core : : CONFIG_FILE_NAME ) ;
QFile file ( path ) ;
if ( file . exists ( ) )
return path ;
2014-10-11 06:07:18 +08:00
else if ( QFile ( path = dir . filePath ( " tox_save " ) ) . exists ( ) ) // also import tox_save if no data
return path ;
2014-10-09 13:30:33 +08:00
else
# endif
2014-10-11 06:07:18 +08:00
{
profile = askProfiles ( ) ;
if ( profile ! = " " )
return dir . filePath ( profile + Core : : TOX_EXT ) ;
else
return " " ;
}
2014-10-09 13:30:33 +08:00
}
else
return path ;
}
QList < QString > Widget : : 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 Widget : : askProfiles ( )
2014-10-11 06:07:18 +08:00
{ // TODO: allow user to create new Tox ID, even if a profile already exists
2014-10-09 13:30:33 +08:00
QList < QString > profiles = searchProfiles ( ) ;
2014-10-11 06:07:18 +08:00
if ( profiles . empty ( ) ) return " " ;
2014-10-09 13:30:33 +08:00
bool ok ;
QString profile = QInputDialog : : getItem ( this ,
tr ( " Choose a profile " ) ,
tr ( " Please choose which identity to use " ) ,
profiles ,
0 , // which slot to start on
false , // if the user can enter their own input
& ok ) ;
if ( ! ok ) // user cancelled
{
qApp - > quit ( ) ;
return " " ;
}
else
return profile ;
}
2014-10-17 02:48:45 +08:00
void Widget : : setIdleTimer ( int minutes )
{
2014-10-17 02:52:13 +08:00
idleTimer - > start ( minutes * 1000 * 60 ) ;
2014-10-17 02:48:45 +08:00
}
2014-06-25 04:11:11 +08:00
QString Widget : : getUsername ( )
{
2014-10-06 14:46:30 +08:00
return core - > getUsername ( ) ;
2014-06-25 04:11:11 +08:00
}
2014-09-24 22:51:16 +08:00
void Widget : : onAvatarClicked ( )
{
2014-09-28 16:59:49 +08:00
QString filename = QFileDialog : : getOpenFileName ( this , tr ( " Choose a profile picture " ) , QDir : : homePath ( ) ) ;
2014-10-18 07:06:30 +08:00
if ( filename . isEmpty ( ) )
2014-09-25 23:16:36 +08:00
return ;
2014-09-24 22:51:16 +08:00
QFile file ( filename ) ;
file . open ( QIODevice : : ReadOnly ) ;
if ( ! file . isOpen ( ) )
{
2014-09-25 23:16:36 +08:00
QMessageBox : : critical ( this , tr ( " Error " ) , tr ( " Unable to open this file " ) ) ;
2014-09-24 22:51:16 +08:00
return ;
}
QPixmap pic ;
if ( ! pic . loadFromData ( file . readAll ( ) ) )
{
2014-09-25 23:16:36 +08:00
QMessageBox : : critical ( this , tr ( " Error " ) , tr ( " Unable to read this image " ) ) ;
2014-09-24 22:51:16 +08:00
return ;
}
QByteArray bytes ;
QBuffer buffer ( & bytes ) ;
buffer . open ( QIODevice : : WriteOnly ) ;
pic . save ( & buffer , " PNG " ) ;
buffer . close ( ) ;
2014-09-25 22:55:36 +08:00
if ( bytes . size ( ) > = TOX_AVATAR_MAX_DATA_LENGTH )
2014-09-24 22:51:16 +08:00
{
2014-09-25 00:40:40 +08:00
pic = pic . scaled ( 64 , 64 , Qt : : KeepAspectRatio , Qt : : SmoothTransformation ) ;
2014-09-24 22:51:16 +08:00
bytes . clear ( ) ;
buffer . open ( QIODevice : : WriteOnly ) ;
pic . save ( & buffer , " PNG " ) ;
buffer . close ( ) ;
}
2014-09-25 22:55:36 +08:00
if ( bytes . size ( ) > = TOX_AVATAR_MAX_DATA_LENGTH )
2014-09-24 22:51:16 +08:00
{
2014-09-25 23:16:36 +08:00
QMessageBox : : critical ( this , tr ( " Error " ) , tr ( " This image is too big " ) ) ;
2014-09-24 22:51:16 +08:00
return ;
}
2014-09-25 22:55:36 +08:00
core - > setAvatar ( TOX_AVATAR_FORMAT_PNG , bytes ) ;
2014-09-24 22:51:16 +08:00
}
void Widget : : onSelfAvatarLoaded ( const QPixmap & pic )
{
2014-09-26 22:39:29 +08:00
profilePicture - > setPixmap ( pic ) ;
2014-09-24 22:51:16 +08:00
}
2014-06-25 04:11:11 +08:00
void Widget : : onConnected ( )
{
2014-09-03 06:01:04 +08:00
ui - > statusButton - > setEnabled ( true ) ;
2014-06-25 04:11:11 +08:00
emit statusSet ( Status : : Online ) ;
}
void Widget : : onDisconnected ( )
{
2014-09-03 06:01:04 +08:00
ui - > statusButton - > setEnabled ( false ) ;
2014-06-25 04:11:11 +08:00
emit statusSet ( Status : : Offline ) ;
}
void Widget : : onFailedToStartCore ( )
{
QMessageBox critical ( this ) ;
2014-10-06 04:49:44 +08:00
critical . setText ( tr ( " Toxcore failed to start, the application will terminate after you close this message. " ) ) ;
2014-06-25 04:11:11 +08:00
critical . setIcon ( QMessageBox : : Critical ) ;
critical . exec ( ) ;
qApp - > quit ( ) ;
}
2014-10-06 04:49:44 +08:00
void Widget : : onBadProxyCore ( )
{
QMessageBox critical ( this ) ;
critical . setText ( tr ( " toxcore failed to start with your proxy settings. qTox cannot run; please modify your "
" settings and restart. " , " popup text " ) ) ;
critical . setIcon ( QMessageBox : : Critical ) ;
critical . exec ( ) ;
onSettingsClicked ( ) ;
}
2014-06-25 04:11:11 +08:00
void Widget : : onStatusSet ( Status status )
{
2014-07-06 06:03:53 +08:00
//We have to use stylesheets here, there's no way to
//prevent the button icon from moving when pressed otherwise
2014-10-04 16:24:20 +08:00
switch ( status )
2014-07-06 06:03:53 +08:00
{
2014-10-04 16:24:20 +08:00
case Status : : Online :
ui - > statusButton - > setProperty ( " status " , " online " ) ;
break ;
case Status : : Away :
ui - > statusButton - > setProperty ( " status " , " away " ) ;
break ;
case Status : : Busy :
ui - > statusButton - > setProperty ( " status " , " busy " ) ;
break ;
case Status : : Offline :
ui - > statusButton - > setProperty ( " status " , " offline " ) ;
break ;
2014-07-06 06:03:53 +08:00
}
2014-10-04 16:24:20 +08:00
Style : : repolish ( ui - > statusButton ) ;
2014-06-25 04:11:11 +08:00
}
void Widget : : onAddClicked ( )
{
hideMainForms ( ) ;
2014-10-18 22:54:52 +08:00
addFriendForm - > show ( * ui ) ;
2014-11-06 06:47:52 +08:00
setWindowTitle ( tr ( " Add friend " ) + " - qTox " ) ;
2014-06-25 04:11:11 +08:00
}
void Widget : : onGroupClicked ( )
{
2014-06-30 05:38:48 +08:00
core - > createGroup ( ) ;
2014-06-25 04:11:11 +08:00
}
void Widget : : onTransferClicked ( )
{
2014-07-12 00:05:18 +08:00
hideMainForms ( ) ;
2014-10-18 22:54:52 +08:00
filesForm - > show ( * ui ) ;
2014-11-06 06:47:52 +08:00
setWindowTitle ( tr ( " File transfers " ) + " - qTox " ) ;
2014-09-02 00:20:28 +08:00
activeChatroomWidget = nullptr ;
2014-06-25 04:11:11 +08:00
}
2014-10-21 02:03:44 +08:00
void Widget : : onIconClick ( QSystemTrayIcon : : ActivationReason reason )
2014-10-08 22:17:05 +08:00
{
2014-10-21 02:03:44 +08:00
switch ( reason ) {
case QSystemTrayIcon : : Trigger :
if ( this - > isHidden ( ) = = true )
this - > show ( ) ;
else
this - > hide ( ) ;
case QSystemTrayIcon : : DoubleClick :
break ;
case QSystemTrayIcon : : MiddleClick :
break ;
default :
;
}
2014-10-08 22:17:05 +08:00
}
2014-06-25 04:11:11 +08:00
void Widget : : onSettingsClicked ( )
{
hideMainForms ( ) ;
2014-09-30 17:44:27 +08:00
settingsWidget - > show ( * ui ) ;
2014-11-06 06:47:52 +08:00
setWindowTitle ( tr ( " Settings " ) + " - qTox " ) ;
2014-09-02 00:20:28 +08:00
activeChatroomWidget = nullptr ;
2014-06-25 04:11:11 +08:00
}
void Widget : : hideMainForms ( )
{
QLayoutItem * item ;
while ( ( item = ui - > mainHead - > layout ( ) - > takeAt ( 0 ) ) ! = 0 )
item - > widget ( ) - > hide ( ) ;
while ( ( item = ui - > mainContent - > layout ( ) - > takeAt ( 0 ) ) ! = 0 )
item - > widget ( ) - > hide ( ) ;
2014-10-04 16:54:50 +08:00
2014-09-02 00:20:28 +08:00
if ( activeChatroomWidget ! = nullptr )
2014-06-27 09:47:16 +08:00
{
2014-09-02 00:20:28 +08:00
activeChatroomWidget - > setAsInactiveChatroom ( ) ;
2014-06-27 09:47:16 +08:00
}
2014-06-25 04:11:11 +08:00
}
void Widget : : onUsernameChanged ( const QString & newUsername , const QString & oldUsername )
{
ui - > nameLabel - > setText ( oldUsername ) ; // restore old username until Core tells us to set it
2014-07-16 00:04:26 +08:00
ui - > nameLabel - > setToolTip ( oldUsername ) ; // for overlength names
2014-06-25 04:11:11 +08:00
core - > setUsername ( newUsername ) ;
}
void Widget : : setUsername ( const QString & username )
{
ui - > nameLabel - > setText ( username ) ;
2014-07-16 00:04:26 +08:00
ui - > nameLabel - > setToolTip ( username ) ; // for overlength names
2014-06-25 04:11:11 +08:00
}
void Widget : : onStatusMessageChanged ( const QString & newStatusMessage , const QString & oldStatusMessage )
{
ui - > statusLabel - > setText ( oldStatusMessage ) ; // restore old status message until Core tells us to set it
2014-07-16 00:04:26 +08:00
ui - > statusLabel - > setToolTip ( oldStatusMessage ) ; // for overlength messsages
2014-06-25 04:11:11 +08:00
core - > setStatusMessage ( newStatusMessage ) ;
}
void Widget : : setStatusMessage ( const QString & statusMessage )
{
ui - > statusLabel - > setText ( statusMessage ) ;
2014-07-16 00:04:26 +08:00
ui - > statusLabel - > setToolTip ( statusMessage ) ; // for overlength messsages
2014-06-25 04:11:11 +08:00
}
void Widget : : addFriend ( int friendId , const QString & userId )
{
2014-11-01 13:30:04 +08:00
//qDebug() << "Widget: Adding friend with id" << userId;
2014-06-25 04:11:11 +08:00
Friend * newfriend = FriendList : : addFriend ( friendId , userId ) ;
2014-08-22 23:19:16 +08:00
QLayout * layout = contactListWidget - > getFriendLayout ( Status : : Offline ) ;
2014-11-06 23:26:22 +08:00
layout - > addWidget ( newfriend - > getFriendWidget ( ) ) ;
2014-11-07 22:55:32 +08:00
2014-11-08 00:02:10 +08:00
QString alias = Settings : : getInstance ( ) . getFriendAlias ( ToxID : : fromString ( userId ) ) ;
newfriend - > setAlias ( alias ) ;
2014-11-07 22:55:32 +08:00
2014-11-06 23:26:22 +08:00
connect ( newfriend - > getFriendWidget ( ) , SIGNAL ( chatroomWidgetClicked ( GenericChatroomWidget * ) ) , this , SLOT ( onChatroomWidgetClicked ( GenericChatroomWidget * ) ) ) ;
connect ( newfriend - > getFriendWidget ( ) , SIGNAL ( removeFriend ( int ) ) , this , SLOT ( removeFriend ( int ) ) ) ;
connect ( newfriend - > getFriendWidget ( ) , SIGNAL ( copyFriendIdToClipboard ( int ) ) , this , SLOT ( copyFriendIdToClipboard ( int ) ) ) ;
connect ( newfriend - > getFriendWidget ( ) , SIGNAL ( chatroomWidgetClicked ( GenericChatroomWidget * ) ) , newfriend - > getChatForm ( ) , SLOT ( focusInput ( ) ) ) ;
connect ( newfriend - > getChatForm ( ) , SIGNAL ( sendMessage ( int , QString ) ) , core , SLOT ( sendMessage ( int , QString ) ) ) ;
connect ( newfriend - > getChatForm ( ) , & GenericChatForm : : sendAction , core , & Core : : sendAction ) ;
connect ( newfriend - > getChatForm ( ) , SIGNAL ( sendFile ( int32_t , QString , QString , long long ) ) , core , SLOT ( sendFile ( int32_t , QString , QString , long long ) ) ) ;
connect ( newfriend - > getChatForm ( ) , SIGNAL ( answerCall ( int ) ) , core , SLOT ( answerCall ( int ) ) ) ;
connect ( newfriend - > getChatForm ( ) , SIGNAL ( hangupCall ( int ) ) , core , SLOT ( hangupCall ( int ) ) ) ;
connect ( newfriend - > getChatForm ( ) , SIGNAL ( startCall ( int ) ) , core , SLOT ( startCall ( int ) ) ) ;
connect ( newfriend - > getChatForm ( ) , SIGNAL ( startVideoCall ( int , bool ) ) , core , SLOT ( startCall ( int , bool ) ) ) ;
connect ( newfriend - > getChatForm ( ) , SIGNAL ( cancelCall ( int , int ) ) , core , SLOT ( cancelCall ( int , int ) ) ) ;
connect ( newfriend - > getChatForm ( ) , SIGNAL ( micMuteToggle ( int ) ) , core , SLOT ( micMuteToggle ( int ) ) ) ;
connect ( newfriend - > getChatForm ( ) , SIGNAL ( volMuteToggle ( int ) ) , core , SLOT ( volMuteToggle ( int ) ) ) ;
connect ( core , & Core : : fileReceiveRequested , newfriend - > getChatForm ( ) , & ChatForm : : onFileRecvRequest ) ;
connect ( core , & Core : : avInvite , newfriend - > getChatForm ( ) , & ChatForm : : onAvInvite ) ;
connect ( core , & Core : : avStart , newfriend - > getChatForm ( ) , & ChatForm : : onAvStart ) ;
connect ( core , & Core : : avCancel , newfriend - > getChatForm ( ) , & ChatForm : : onAvCancel ) ;
connect ( core , & Core : : avEnd , newfriend - > getChatForm ( ) , & ChatForm : : onAvEnd ) ;
connect ( core , & Core : : avRinging , newfriend - > getChatForm ( ) , & ChatForm : : onAvRinging ) ;
connect ( core , & Core : : avStarting , newfriend - > getChatForm ( ) , & ChatForm : : onAvStarting ) ;
connect ( core , & Core : : avEnding , newfriend - > getChatForm ( ) , & ChatForm : : onAvEnding ) ;
connect ( core , & Core : : avRequestTimeout , newfriend - > getChatForm ( ) , & ChatForm : : onAvRequestTimeout ) ;
connect ( core , & Core : : avPeerTimeout , newfriend - > getChatForm ( ) , & ChatForm : : onAvPeerTimeout ) ;
connect ( core , & Core : : avMediaChange , newfriend - > getChatForm ( ) , & ChatForm : : onAvMediaChange ) ;
connect ( core , & Core : : avCallFailed , newfriend - > getChatForm ( ) , & ChatForm : : onAvCallFailed ) ;
connect ( core , & Core : : avRejected , newfriend - > getChatForm ( ) , & ChatForm : : onAvRejected ) ;
connect ( core , & Core : : friendAvatarChanged , newfriend - > getChatForm ( ) , & ChatForm : : onAvatarChange ) ;
connect ( core , & Core : : friendAvatarChanged , newfriend - > getFriendWidget ( ) , & FriendWidget : : onAvatarChange ) ;
connect ( core , & Core : : friendAvatarRemoved , newfriend - > getChatForm ( ) , & ChatForm : : onAvatarRemoved ) ;
connect ( core , & Core : : friendAvatarRemoved , newfriend - > getFriendWidget ( ) , & FriendWidget : : onAvatarRemoved ) ;
2014-09-24 23:55:54 +08:00
// Try to get the avatar from the cache
2014-09-28 06:19:30 +08:00
QPixmap avatar = Settings : : getInstance ( ) . getSavedAvatar ( userId ) ;
2014-09-24 23:55:54 +08:00
if ( ! avatar . isNull ( ) )
{
2014-11-01 13:30:04 +08:00
//qWarning() << "Widget: loadded avatar for id" << userId;
2014-11-06 23:26:22 +08:00
newfriend - > getChatForm ( ) - > onAvatarChange ( friendId , avatar ) ;
newfriend - > getFriendWidget ( ) - > onAvatarChange ( friendId , avatar ) ;
2014-09-24 23:55:54 +08:00
}
2014-06-27 01:45:47 +08:00
}
2014-11-10 03:23:43 +08:00
void Widget : : addFriendFailed ( const QString & , const QString & errorInfo )
2014-06-27 01:45:47 +08:00
{
2014-11-10 08:04:35 +08:00
QString info = QString ( tr ( " Couldn't request friendship " ) ) ;
2014-11-10 03:23:43 +08:00
if ( ! errorInfo . isEmpty ( ) ) {
info = info + ( QString ( " : " ) + errorInfo ) ;
}
QMessageBox : : critical ( 0 , " Error " , info ) ;
2014-06-25 04:11:11 +08:00
}
void Widget : : onFriendStatusChanged ( int friendId , Status status )
{
Friend * f = FriendList : : findFriend ( friendId ) ;
if ( ! f )
return ;
2014-11-06 23:26:22 +08:00
contactListWidget - > moveWidget ( f - > getFriendWidget ( ) , status ) ;
2014-08-22 23:19:16 +08:00
2014-11-09 09:01:02 +08:00
bool isActualChange = f - > getStatus ( ) ! = status ;
2014-11-06 23:26:22 +08:00
f - > setStatus ( status ) ;
f - > getFriendWidget ( ) - > updateStatusLight ( ) ;
2014-10-11 07:18:32 +08:00
2014-10-18 07:06:30 +08:00
//won't print the message if there were no messages before
2014-11-08 21:46:26 +08:00
if ( ! f - > getChatForm ( ) - > isEmpty ( )
2014-11-10 16:09:20 +08:00
& & Settings : : getInstance ( ) . getStatusChangeNotificationEnabled ( ) )
2014-10-18 07:06:30 +08:00
{
QString fStatus = " " ;
2014-11-06 23:26:22 +08:00
switch ( f - > getStatus ( ) ) {
2014-10-18 07:06:30 +08:00
case Status : : Away :
fStatus = tr ( " away " , " contact status " ) ; break ;
case Status : : Busy :
fStatus = tr ( " busy " , " contact status " ) ; break ;
case Status : : Offline :
fStatus = tr ( " offline " , " contact status " ) ; break ;
default :
fStatus = tr ( " online " , " contact status " ) ; break ;
}
2014-11-09 09:01:02 +08:00
if ( isActualChange )
f - > getChatForm ( ) - > addSystemInfoMessage ( tr ( " %1 is now %2 " , " e.g. \" Dubslow is now online \" " ) . arg ( f - > getDisplayedName ( ) ) . arg ( fStatus ) ,
2014-10-18 07:06:30 +08:00
" white " , QDateTime : : currentDateTime ( ) ) ;
}
2014-06-25 04:11:11 +08:00
}
void Widget : : onFriendStatusMessageChanged ( int friendId , const QString & message )
{
Friend * f = FriendList : : findFriend ( friendId ) ;
if ( ! f )
return ;
2014-09-29 02:09:10 +08:00
QString str = message ; str . replace ( ' \n ' , ' ' ) ;
str . remove ( ' \r ' ) ; str . remove ( QChar ( ( char ) 0 ) ) ; // null terminator...
f - > setStatusMessage ( str ) ;
2014-06-25 04:11:11 +08:00
}
void Widget : : onFriendUsernameChanged ( int friendId , const QString & username )
{
Friend * f = FriendList : : findFriend ( friendId ) ;
if ( ! f )
return ;
2014-09-29 02:09:10 +08:00
QString str = username ; str . replace ( ' \n ' , ' ' ) ;
str . remove ( ' \r ' ) ; str . remove ( QChar ( ( char ) 0 ) ) ; // null terminator...
f - > setName ( str ) ;
2014-06-25 04:11:11 +08:00
}
2014-09-02 00:20:28 +08:00
void Widget : : onChatroomWidgetClicked ( GenericChatroomWidget * widget )
2014-06-25 04:11:11 +08:00
{
hideMainForms ( ) ;
2014-09-02 00:20:28 +08:00
widget - > setChatForm ( * ui ) ;
if ( activeChatroomWidget ! = nullptr )
2014-06-27 09:47:16 +08:00
{
2014-09-02 00:20:28 +08:00
activeChatroomWidget - > setAsInactiveChatroom ( ) ;
2014-06-27 09:47:16 +08:00
}
2014-09-02 00:20:28 +08:00
activeChatroomWidget = widget ;
2014-06-27 09:47:16 +08:00
widget - > setAsActiveChatroom ( ) ;
2014-11-06 06:47:52 +08:00
setWindowTitle ( widget - > getName ( ) + " - qTox " ) ;
2014-09-02 00:20:28 +08:00
widget - > resetEventFlags ( ) ;
widget - > updateStatusLight ( ) ;
2014-06-25 04:11:11 +08:00
}
2014-10-02 14:01:23 +08:00
void Widget : : onFriendMessageReceived ( int friendId , const QString & message , bool isAction )
2014-06-25 04:11:11 +08:00
{
Friend * f = FriendList : : findFriend ( friendId ) ;
if ( ! f )
return ;
2014-10-14 13:49:27 +08:00
QDateTime timestamp = QDateTime : : currentDateTime ( ) ;
2014-11-06 23:26:22 +08:00
f - > getChatForm ( ) - > addMessage ( f - > getToxID ( ) , message , isAction , timestamp ) ;
2014-10-14 22:36:17 +08:00
if ( isAction )
2014-11-07 13:29:23 +08:00
HistoryKeeper : : getInstance ( ) - > addChatEntry ( f - > getToxID ( ) . publicKey , " /me " + message , f - > getToxID ( ) . publicKey , timestamp ) ;
2014-10-14 22:36:17 +08:00
else
2014-11-07 13:29:23 +08:00
HistoryKeeper : : getInstance ( ) - > addChatEntry ( f - > getToxID ( ) . publicKey , message , f - > getToxID ( ) . publicKey , timestamp ) ;
2014-06-28 00:12:43 +08:00
2014-09-02 00:20:28 +08:00
if ( activeChatroomWidget ! = nullptr )
2014-06-28 00:12:43 +08:00
{
2014-11-06 23:26:22 +08:00
if ( ( static_cast < GenericChatroomWidget * > ( f - > getFriendWidget ( ) ) ! = activeChatroomWidget ) | | isMinimized ( ) | | ! isActiveWindow ( ) )
2014-06-28 05:36:44 +08:00
{
2014-11-06 23:26:22 +08:00
f - > setEventFlag ( true ) ;
2014-11-10 16:09:20 +08:00
newMessageAlert ( f - > getFriendWidget ( ) ) ;
2014-06-28 05:36:44 +08:00
}
2014-06-28 00:12:43 +08:00
}
else
2014-06-28 05:36:44 +08:00
{
2014-11-06 23:26:22 +08:00
f - > setEventFlag ( true ) ;
2014-11-10 16:09:20 +08:00
newMessageAlert ( f - > getFriendWidget ( ) ) ;
2014-06-28 05:36:44 +08:00
}
2014-06-28 00:12:43 +08:00
2014-11-06 23:26:22 +08:00
f - > getFriendWidget ( ) - > updateStatusLight ( ) ;
2014-06-25 04:11:11 +08:00
}
2014-11-10 16:09:20 +08:00
void Widget : : newMessageAlert ( GenericChatroomWidget * chat )
2014-06-28 05:36:44 +08:00
{
2014-07-01 10:25:00 +08:00
QApplication : : alert ( this ) ;
2014-08-30 21:15:23 +08:00
2014-09-11 03:53:18 +08:00
static QFile sndFile ( " :audio/notification.pcm " ) ;
2014-11-10 16:09:20 +08:00
if ( ( isMinimized ( ) | | ! isActiveWindow ( ) ) & & Settings : : getInstance ( ) . getShowInFront ( ) )
2014-11-10 08:23:41 +08:00
{
this - > show ( ) ;
2014-11-10 17:22:20 +08:00
showNormal ( ) ;
activateWindow ( ) ;
2014-11-10 16:09:20 +08:00
emit chat - > chatroomWidgetClicked ( chat ) ;
2014-11-10 08:23:41 +08:00
}
2014-08-30 21:15:23 +08:00
static QByteArray sndData ;
if ( sndData . isEmpty ( ) )
{
sndFile . open ( QIODevice : : ReadOnly ) ;
sndData = sndFile . readAll ( ) ;
sndFile . close ( ) ;
}
ALuint buffer ;
alGenBuffers ( 1 , & buffer ) ;
2014-09-11 03:53:18 +08:00
alBufferData ( buffer , AL_FORMAT_MONO16 , sndData . data ( ) , sndData . size ( ) , 44100 ) ;
2014-08-30 21:15:23 +08:00
alSourcei ( core - > alMainSource , AL_BUFFER , buffer ) ;
alSourcePlay ( core - > alMainSource ) ;
2014-06-28 05:36:44 +08:00
}
2014-10-08 10:20:38 +08:00
void Widget : : playRingtone ( )
{
QApplication : : alert ( this ) ;
static QFile sndFile1 ( " :audio/ToxicIncomingCall.pcm " ) ; // for whatever reason this plays slower/downshifted from what any other program plays the file as... but whatever
static QByteArray sndData1 ;
if ( sndData1 . isEmpty ( ) )
{
sndFile1 . open ( QIODevice : : ReadOnly ) ;
sndData1 = sndFile1 . readAll ( ) ;
sndFile1 . close ( ) ;
}
ALuint buffer ;
alGenBuffers ( 1 , & buffer ) ;
alBufferData ( buffer , AL_FORMAT_MONO16 , sndData1 . data ( ) , sndData1 . size ( ) , 44100 ) ;
alSourcei ( core - > alMainSource , AL_BUFFER , buffer ) ;
alSourcePlay ( core - > alMainSource ) ;
}
2014-06-25 04:11:11 +08:00
void Widget : : onFriendRequestReceived ( const QString & userId , const QString & message )
{
FriendRequestDialog dialog ( this , userId , message ) ;
if ( dialog . exec ( ) = = QDialog : : Accepted )
emit friendRequestAccepted ( userId ) ;
}
2014-07-23 17:22:09 +08:00
void Widget : : removeFriend ( Friend * f )
2014-06-25 04:11:11 +08:00
{
2014-11-06 23:26:22 +08:00
f - > getFriendWidget ( ) - > setAsInactiveChatroom ( ) ;
if ( static_cast < GenericChatroomWidget * > ( f - > getFriendWidget ( ) ) = = activeChatroomWidget )
2014-09-02 00:20:28 +08:00
activeChatroomWidget = nullptr ;
2014-11-06 23:26:22 +08:00
FriendList : : removeFriend ( f - > getFriendID ( ) ) ;
core - > removeFriend ( f - > getFriendID ( ) ) ;
2014-06-25 04:11:11 +08:00
delete f ;
if ( ui - > mainHead - > layout ( ) - > isEmpty ( ) )
onAddClicked ( ) ;
2014-11-02 16:45:44 +08:00
contactListWidget - > hide ( ) ;
contactListWidget - > show ( ) ;
2014-06-25 04:11:11 +08:00
}
2014-07-23 17:22:09 +08:00
void Widget : : removeFriend ( int friendId )
{
removeFriend ( FriendList : : findFriend ( friendId ) ) ;
}
2014-10-09 16:07:26 +08:00
void Widget : : clearContactsList ( )
{
2014-11-07 22:55:32 +08:00
QList < Friend * > friends = FriendList : : getAllFriends ( ) ;
for ( Friend * f : friends )
2014-10-09 16:07:26 +08:00
removeFriend ( f ) ;
for ( Group * g : GroupList : : groupList )
removeGroup ( g ) ;
}
2014-07-02 05:50:07 +08:00
void Widget : : copyFriendIdToClipboard ( int friendId )
{
Friend * f = FriendList : : findFriend ( friendId ) ;
if ( f ! = nullptr )
{
QClipboard * clipboard = QApplication : : clipboard ( ) ;
2014-11-06 23:26:22 +08:00
clipboard - > setText ( core - > getFriendAddress ( f - > getFriendID ( ) ) , QClipboard : : Clipboard ) ;
2014-07-02 05:50:07 +08:00
}
}
2014-09-28 16:56:02 +08:00
void Widget : : onGroupInviteReceived ( int32_t friendId , const uint8_t * publicKey , uint16_t length )
2014-06-25 04:11:11 +08:00
{
2014-09-28 16:56:02 +08:00
int groupId = core - > joinGroupchat ( friendId , publicKey , length ) ;
2014-06-25 04:11:11 +08:00
if ( groupId = = - 1 )
{
qWarning ( ) < < " Widget::onGroupInviteReceived: Unable to accept invitation " ;
return ;
}
}
2014-11-01 21:00:43 +08:00
void Widget : : onGroupMessageReceived ( int groupnumber , const QString & message , const QString & author , bool isAction )
2014-06-25 04:11:11 +08:00
{
Group * g = GroupList : : findGroup ( groupnumber ) ;
if ( ! g )
return ;
2014-10-20 19:05:45 +08:00
QString name = core - > getUsername ( ) ;
2014-11-01 21:00:43 +08:00
2014-10-20 19:05:45 +08:00
bool targeted = ( author ! = name ) & & message . contains ( name , Qt : : CaseInsensitive ) ;
if ( targeted )
2014-10-22 22:05:04 +08:00
g - > chatForm - > addAlertMessage ( author , message , QDateTime : : currentDateTime ( ) ) ;
2014-10-20 19:05:45 +08:00
else
2014-11-01 21:00:43 +08:00
g - > chatForm - > addMessage ( author , message , isAction , QDateTime : : currentDateTime ( ) ) ;
2014-06-28 07:10:02 +08:00
2014-10-04 16:54:50 +08:00
if ( ( static_cast < GenericChatroomWidget * > ( g - > widget ) ! = activeChatroomWidget ) | | isMinimized ( ) | | ! isActiveWindow ( ) )
2014-06-28 07:10:02 +08:00
{
2014-09-02 00:20:28 +08:00
g - > hasNewMessages = 1 ;
2014-10-20 19:05:45 +08:00
if ( targeted )
2014-06-28 07:10:02 +08:00
{
2014-11-10 16:09:20 +08:00
newMessageAlert ( g - > widget ) ;
2014-10-06 14:46:30 +08:00
g - > userWasMentioned = 1 ; // useful for highlighting line or desktop notifications
2014-06-28 07:10:02 +08:00
}
2014-09-02 00:20:28 +08:00
g - > widget - > updateStatusLight ( ) ;
2014-06-28 07:10:02 +08:00
}
2014-06-25 04:11:11 +08:00
}
void Widget : : onGroupNamelistChanged ( int groupnumber , int peernumber , uint8_t Change )
{
Group * g = GroupList : : findGroup ( groupnumber ) ;
if ( ! g )
{
qDebug ( ) < < " Widget::onGroupNamelistChanged: Group not found, creating it " ;
g = createGroup ( groupnumber ) ;
}
2014-10-20 19:34:17 +08:00
QString name = core - > getGroupPeerName ( groupnumber , peernumber ) ;
2014-06-25 04:11:11 +08:00
TOX_CHAT_CHANGE change = static_cast < TOX_CHAT_CHANGE > ( Change ) ;
if ( change = = TOX_CHAT_CHANGE_PEER_ADD )
2014-09-12 00:05:02 +08:00
{
if ( name . isEmpty ( ) )
name = tr ( " <Unknown> " , " Placeholder when we don't know someone's name in a group chat " ) ;
g - > addPeer ( peernumber , name ) ;
2014-10-20 19:34:17 +08:00
//g->chatForm->addSystemInfoMessage(tr("%1 has joined the chat").arg(name), "green");
// we can't display these messages until irungentoo fixes peernumbers
// https://github.com/irungentoo/toxcore/issues/1128
2014-09-12 00:05:02 +08:00
}
2014-06-25 04:11:11 +08:00
else if ( change = = TOX_CHAT_CHANGE_PEER_DEL )
2014-10-20 19:34:17 +08:00
{
2014-06-25 04:11:11 +08:00
g - > removePeer ( peernumber ) ;
2014-10-20 19:34:17 +08:00
//g->chatForm->addSystemInfoMessage(tr("%1 has left the chat").arg(name), "silver");
}
else if ( change = = TOX_CHAT_CHANGE_PEER_NAME ) // core overwrites old name before telling us it changed...
2014-06-25 04:11:11 +08:00
g - > updatePeer ( peernumber , core - > getGroupPeerName ( groupnumber , peernumber ) ) ;
}
2014-10-09 16:07:26 +08:00
void Widget : : removeGroup ( Group * g )
2014-06-25 04:11:11 +08:00
{
2014-07-03 14:43:23 +08:00
g - > widget - > setAsInactiveChatroom ( ) ;
2014-09-02 00:20:28 +08:00
if ( static_cast < GenericChatroomWidget * > ( g - > widget ) = = activeChatroomWidget )
2014-10-30 17:44:26 +08:00
{
2014-09-02 00:20:28 +08:00
activeChatroomWidget = nullptr ;
2014-10-30 17:44:26 +08:00
onAddClicked ( ) ;
}
2014-10-09 16:07:26 +08:00
GroupList : : removeGroup ( g - > groupId ) ;
core - > removeGroup ( g - > groupId ) ;
2014-06-25 04:11:11 +08:00
delete g ;
if ( ui - > mainHead - > layout ( ) - > isEmpty ( ) )
onAddClicked ( ) ;
2014-11-02 16:45:44 +08:00
contactListWidget - > hide ( ) ;
contactListWidget - > show ( ) ;
2014-06-25 04:11:11 +08:00
}
2014-10-09 16:07:26 +08:00
void Widget : : removeGroup ( int groupId )
{
removeGroup ( GroupList : : findGroup ( groupId ) ) ;
}
2014-06-26 07:48:20 +08:00
Core * Widget : : getCore ( )
2014-06-25 04:11:11 +08:00
{
return core ;
}
Group * Widget : : createGroup ( int groupId )
{
Group * g = GroupList : : findGroup ( groupId ) ;
if ( g )
{
qWarning ( ) < < " Widget::createGroup: Group already exists " ;
return g ;
}
QString groupName = QString ( " Groupchat #%1 " ) . arg ( groupId ) ;
Group * newgroup = GroupList : : addGroup ( groupId , groupName ) ;
2014-08-22 23:19:16 +08:00
QLayout * layout = contactListWidget - > getGroupLayout ( ) ;
2014-06-25 04:11:11 +08:00
layout - > addWidget ( newgroup - > widget ) ;
2014-09-02 00:20:28 +08:00
newgroup - > widget - > updateStatusLight ( ) ;
2014-08-11 16:00:08 +08:00
2014-09-02 00:20:28 +08:00
connect ( newgroup - > widget , SIGNAL ( chatroomWidgetClicked ( GenericChatroomWidget * ) ) , this , SLOT ( onChatroomWidgetClicked ( GenericChatroomWidget * ) ) ) ;
2014-06-25 04:11:11 +08:00
connect ( newgroup - > widget , SIGNAL ( removeGroup ( int ) ) , this , SLOT ( removeGroup ( int ) ) ) ;
2014-09-11 05:00:45 +08:00
connect ( newgroup - > widget , SIGNAL ( chatroomWidgetClicked ( GenericChatroomWidget * ) ) , newgroup - > chatForm , SLOT ( focusInput ( ) ) ) ;
2014-06-25 04:11:11 +08:00
connect ( newgroup - > chatForm , SIGNAL ( sendMessage ( int , QString ) ) , core , SLOT ( sendGroupMessage ( int , QString ) ) ) ;
2014-11-01 21:00:43 +08:00
connect ( newgroup - > chatForm , SIGNAL ( sendAction ( int , QString ) ) , core , SLOT ( sendGroupAction ( int , QString ) ) ) ;
2014-06-25 04:11:11 +08:00
return newgroup ;
}
2014-06-30 05:38:48 +08:00
void Widget : : onEmptyGroupCreated ( int groupId )
{
createGroup ( groupId ) ;
}
2014-07-01 03:14:51 +08:00
bool Widget : : isFriendWidgetCurActiveWidget ( Friend * f )
{
if ( ! f )
return false ;
2014-09-02 00:20:28 +08:00
2014-11-06 23:26:22 +08:00
return ( activeChatroomWidget = = static_cast < GenericChatroomWidget * > ( f - > getFriendWidget ( ) ) ) ;
2014-07-01 03:14:51 +08:00
}
2014-07-01 10:12:17 +08:00
2014-07-03 06:27:30 +08:00
bool Widget : : event ( QEvent * e )
2014-07-01 10:12:17 +08:00
{
2014-10-16 17:47:58 +08:00
switch ( e - > type ( ) ) {
case QEvent : : WindowActivate :
if ( activeChatroomWidget ! = nullptr )
{
activeChatroomWidget - > resetEventFlags ( ) ;
activeChatroomWidget - > updateStatusLight ( ) ;
}
// http://qt-project.org/faq/answer/how_can_i_detect_a_period_of_no_user_interaction
// Detecting global inactivity, like Skype, is possible but not via Qt:
// http://stackoverflow.com/a/21905027/1497645
case QEvent : : MouseButtonPress :
case QEvent : : MouseButtonRelease :
case QEvent : : Wheel :
case QEvent : : KeyPress :
case QEvent : : KeyRelease :
if ( autoAwayActive )
{
qDebug ( ) < < " Widget: auto away deactivated " ;
autoAwayActive = false ;
emit statusSet ( Status : : Online ) ;
int mins = Settings : : getInstance ( ) . getAutoAwayTime ( ) ;
if ( mins > 0 )
idleTimer - > start ( mins * 1000 * 60 ) ;
}
default :
break ;
2014-07-03 06:27:30 +08:00
}
2014-07-01 10:12:17 +08:00
return QWidget : : event ( e ) ;
}
2014-10-16 17:47:58 +08:00
void Widget : : onUserAway ( )
{
if ( Settings : : getInstance ( ) . getAutoAwayTime ( ) > 0
& & ui - > statusButton - > property ( " status " ) . toString ( ) = = " online " ) // leave user-set statuses in place
{
qDebug ( ) < < " Widget: auto away activated " ;
emit statusSet ( Status : : Away ) ;
autoAwayActive = true ;
}
idleTimer - > stop ( ) ;
}
2014-07-06 04:54:27 +08:00
void Widget : : setStatusOnline ( )
{
core - > setStatus ( Status : : Online ) ;
}
void Widget : : setStatusAway ( )
{
core - > setStatus ( Status : : Away ) ;
}
void Widget : : setStatusBusy ( )
2014-07-04 01:33:08 +08:00
{
2014-07-06 04:54:27 +08:00
core - > setStatus ( Status : : Busy ) ;
2014-07-04 01:33:08 +08:00
}
2014-07-21 02:17:06 +08:00
2014-09-24 00:35:06 +08:00
void Widget : : onMessageSendResult ( int friendId , const QString & message , int messageId )
{
Q_UNUSED ( message )
Friend * f = FriendList : : findFriend ( friendId ) ;
if ( ! f )
return ;
if ( ! messageId )
2014-11-06 23:26:22 +08:00
f - > getChatForm ( ) - > addSystemInfoMessage ( tr ( " Message failed to send " ) , " red " , QDateTime : : currentDateTime ( ) ) ;
2014-09-24 00:35:06 +08:00
}
void Widget : : onGroupSendResult ( int groupId , const QString & message , int result )
{
Q_UNUSED ( message )
Group * g = GroupList : : findGroup ( groupId ) ;
if ( ! g )
return ;
if ( result = = - 1 )
2014-10-29 18:13:32 +08:00
g - > chatForm - > addSystemInfoMessage ( tr ( " Message failed to send " ) , " red " , QDateTime : : currentDateTime ( ) ) ;
2014-09-24 00:35:06 +08:00
}
2014-10-16 00:18:32 +08:00
2014-10-24 19:25:40 +08:00
void Widget : : getPassword ( QString info , int passtype , uint8_t * salt )
2014-10-16 00:18:32 +08:00
{
2014-10-22 21:36:36 +08:00
Core : : PasswordType pt = static_cast < Core : : PasswordType > ( passtype ) ;
2014-10-19 15:35:11 +08:00
InputPasswordDialog dialog ( info ) ;
2014-10-19 01:38:47 +08:00
if ( dialog . exec ( ) )
{
QString pswd = dialog . getPassword ( ) ;
if ( pswd . isEmpty ( ) )
2014-10-22 21:36:36 +08:00
core - > clearPassword ( pt ) ;
2014-10-19 01:38:47 +08:00
else
2014-10-24 19:25:40 +08:00
core - > setPassword ( pswd , pt , salt ) ;
2014-10-19 01:38:47 +08:00
}
2014-09-24 00:35:06 +08:00
}
2014-10-24 02:39:28 +08:00
QMessageBox : : StandardButton Widget : : showWarningMsgBox ( const QString & title , const QString & msg , QMessageBox : : StandardButtons buttons )
{
// We can only display widgets from the GUI thread
if ( QThread : : currentThread ( ) ! = qApp - > thread ( ) )
{
QMessageBox : : StandardButton ret ;
QMetaObject : : invokeMethod ( this , " showWarningMsgBox " , Qt : : BlockingQueuedConnection ,
Q_RETURN_ARG ( QMessageBox : : StandardButton , ret ) ,
Q_ARG ( const QString & , title ) , Q_ARG ( const QString & , msg ) ,
Q_ARG ( QMessageBox : : StandardButtons , buttons ) ) ;
return ret ;
}
else
{
return QMessageBox : : warning ( this , title , msg , buttons ) ;
}
}
2014-11-02 19:24:14 +08:00
void Widget : : setEnabledThreadsafe ( bool enabled )
{
// We can only do this from the GUI thread
if ( QThread : : currentThread ( ) ! = qApp - > thread ( ) )
{
QMetaObject : : invokeMethod ( this , " setEnabledThreadsafe " , Qt : : BlockingQueuedConnection ,
Q_ARG ( bool , enabled ) ) ;
return ;
}
else
{
return setEnabled ( enabled ) ;
}
}
2014-11-10 07:31:29 +08:00
bool Widget : : askMsgboxQuestion ( const QString & title , const QString & msg )
{
// We can only display widgets from the GUI thread
if ( QThread : : currentThread ( ) ! = qApp - > thread ( ) )
{
bool ret ;
QMetaObject : : invokeMethod ( this , " askMsgboxQuestion " , Qt : : BlockingQueuedConnection ,
Q_RETURN_ARG ( bool , ret ) ,
Q_ARG ( const QString & , title ) , Q_ARG ( const QString & , msg ) ) ;
return ret ;
}
else
{
return QMessageBox : : question ( this , title , msg ) = = QMessageBox : : StandardButton : : Yes ;
}
}