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 "addfriendform.h"
2014-07-01 02:05:01 +08:00
2014-06-25 04:11:11 +08:00
# include <QFont>
2014-07-01 02:05:01 +08:00
# include <QMessageBox>
2014-11-07 06:50:36 +08:00
# include <QErrorMessage>
2014-07-06 08:29:57 +08:00
# include <tox/tox.h>
2014-09-11 21:44:34 +08:00
# include "ui_mainwindow.h"
2015-02-24 21:10:51 +08:00
# include "src/nexus.h"
2014-10-08 12:26:25 +08:00
# include "src/core.h"
2014-11-06 06:27:51 +08:00
# include "src/misc/cdata.h"
2014-11-06 22:12:10 +08:00
# include "src/toxdns.h"
2014-11-07 06:50:36 +08:00
# include "src/misc/settings.h"
2014-11-06 06:27:51 +08:00
2014-11-06 22:12:10 +08:00
AddFriendForm : : AddFriendForm ( )
2014-11-06 06:27:51 +08:00
{
2014-06-25 04:11:11 +08:00
main = new QWidget ( ) , head = new QWidget ( ) ;
QFont bold ;
bold . setBold ( true ) ;
2014-07-04 03:42:23 +08:00
headLabel . setText ( tr ( " Add Friends " ) ) ;
2014-06-25 04:11:11 +08:00
headLabel . setFont ( bold ) ;
2014-07-04 03:42:23 +08:00
toxIdLabel . setText ( tr ( " Tox ID " , " Tox ID of the person you're sending a friend request to " ) ) ;
messageLabel . setText ( tr ( " Message " , " The message you send in friend requests " ) ) ;
sendButton . setText ( tr ( " Send friend request " ) ) ;
2014-06-25 04:11:11 +08:00
main - > setLayout ( & layout ) ;
layout . addWidget ( & toxIdLabel ) ;
layout . addWidget ( & toxId ) ;
layout . addWidget ( & messageLabel ) ;
layout . addWidget ( & message ) ;
layout . addWidget ( & sendButton ) ;
head - > setLayout ( & headLayout ) ;
headLayout . addWidget ( & headLabel ) ;
connect ( & sendButton , SIGNAL ( clicked ( ) ) , this , SLOT ( onSendTriggered ( ) ) ) ;
2015-02-24 21:10:51 +08:00
connect ( Nexus : : getCore ( ) , & Core : : usernameSet , this , & AddFriendForm : : onUsernameSet ) ;
2014-07-01 02:05:01 +08:00
}
AddFriendForm : : ~ AddFriendForm ( )
{
head - > deleteLater ( ) ;
main - > deleteLater ( ) ;
2014-06-25 04:11:11 +08:00
}
2014-08-11 16:00:08 +08:00
void AddFriendForm : : show ( Ui : : MainWindow & ui )
2014-06-25 04:11:11 +08:00
{
ui . mainContent - > layout ( ) - > addWidget ( main ) ;
ui . mainHead - > layout ( ) - > addWidget ( head ) ;
main - > show ( ) ;
head - > show ( ) ;
}
2014-11-06 22:12:10 +08:00
QString AddFriendForm : : getMessage ( ) const
2014-07-01 02:05:01 +08:00
{
2014-11-06 22:12:10 +08:00
const QString msg = message . toPlainText ( ) ;
return ! msg . isEmpty ( ) ? msg : message . placeholderText ( ) ;
2014-07-01 02:05:01 +08:00
}
void AddFriendForm : : showWarning ( const QString & message ) const
{
QMessageBox warning ( main ) ;
2014-09-11 19:40:27 +08:00
warning . setWindowTitle ( " Tox " ) ;
2014-07-01 02:05:01 +08:00
warning . setText ( message ) ;
warning . setIcon ( QMessageBox : : Warning ) ;
warning . exec ( ) ;
}
2015-02-24 21:10:51 +08:00
void AddFriendForm : : onUsernameSet ( const QString & username )
{
message . setPlaceholderText ( tr ( " %1 here! Tox me maybe? " , " Default message in friend requests if the field is left blank. Write something appropriate! " ) . arg ( username ) ) ;
}
2014-06-25 04:11:11 +08:00
void AddFriendForm : : onSendTriggered ( )
{
2014-07-01 02:33:43 +08:00
QString id = toxId . text ( ) . trimmed ( ) ;
2014-07-01 02:05:01 +08:00
if ( id . isEmpty ( ) ) {
2014-07-04 03:42:23 +08:00
showWarning ( tr ( " Please fill in a valid Tox ID " , " Tox ID of the friend you're sending a friend request to " ) ) ;
2014-11-06 22:12:10 +08:00
} else if ( ToxID : : isToxId ( id ) ) {
2014-09-12 21:41:41 +08:00
if ( id . toUpper ( ) = = Core : : getInstance ( ) - > getSelfId ( ) . toString ( ) . toUpper ( ) )
2014-09-13 05:55:37 +08:00
showWarning ( tr ( " You can't add yourself as a friend! " , " When trying to add your own Tox ID as friend " ) ) ;
2014-09-11 19:40:27 +08:00
else
emit friendRequested ( id , getMessage ( ) ) ;
2014-11-06 06:27:51 +08:00
this - > toxId . clear ( ) ;
this - > message . clear ( ) ;
2014-07-01 02:05:01 +08:00
} else {
2014-12-28 20:32:25 +08:00
if ( Settings : : getInstance ( ) . getProxyType ( ) ! = ProxyType : : ptNone )
2014-11-07 06:50:36 +08:00
{
2014-11-09 20:27:08 +08:00
QMessageBox : : StandardButton btn = QMessageBox : : warning ( main , " qTox " , tr ( " qTox needs to use the Tox DNS, but can't do it through a proxy. \n \
2015-01-23 03:21:07 +08:00
Ignore the proxy and connect to the Internet directly ? " ), QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
if ( btn ! = QMessageBox : : Yes )
2014-11-07 06:50:36 +08:00
return ;
}
2014-11-06 22:12:10 +08:00
ToxID toxId = ToxDNS : : resolveToxAddress ( id , true ) ;
2014-06-25 04:11:11 +08:00
2014-11-06 22:12:10 +08:00
if ( toxId . toString ( ) . isEmpty ( ) )
2014-11-06 06:27:51 +08:00
{
2014-11-06 22:12:10 +08:00
showWarning ( tr ( " This Tox ID does not exist " , " DNS error " ) ) ;
return ;
2014-11-06 06:27:51 +08:00
}
2014-11-06 22:12:10 +08:00
emit friendRequested ( toxId . toString ( ) , getMessage ( ) ) ;
this - > toxId . clear ( ) ;
this - > message . clear ( ) ;
2014-11-06 06:27:51 +08:00
}
2014-06-25 04:11:11 +08:00
}