2014-11-08 05:45:21 +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-11-06 22:12:10 +08:00
# include "src/widget/toxuri.h"
# include "src/toxdns.h"
# include "src/widget/tool/friendrequestdialog.h"
2015-02-24 21:10:51 +08:00
# include "src/nexus.h"
2014-11-06 22:12:10 +08:00
# include "src/core.h"
# include <QByteArray>
# include <QString>
# include <QMessageBox>
# include <QVBoxLayout>
# include <QDialogButtonBox>
# include <QLabel>
# include <QLineEdit>
# include <QPlainTextEdit>
# include <QPushButton>
# include <QCoreApplication>
2015-02-20 20:26:41 +08:00
bool toxURIEventHandler ( const QByteArray & eventData )
2014-11-06 22:12:10 +08:00
{
if ( ! eventData . startsWith ( " tox: " ) )
2015-02-20 20:26:41 +08:00
return false ;
2014-11-06 22:12:10 +08:00
handleToxURI ( eventData ) ;
2015-02-20 20:26:41 +08:00
return true ;
2014-11-06 22:12:10 +08:00
}
2015-02-20 20:26:41 +08:00
bool handleToxURI ( const QString & toxURI )
2014-11-06 22:12:10 +08:00
{
Core * core = Core : : getInstance ( ) ;
while ( ! core )
{
core = Core : : getInstance ( ) ;
qApp - > processEvents ( ) ;
}
while ( ! core - > isReady ( ) )
{
qApp - > processEvents ( ) ;
}
2014-11-06 22:26:48 +08:00
QString toxaddr ;
if ( toxURI . startsWith ( " tox:// " ) )
toxaddr = toxURI . mid ( 6 ) ;
else
toxaddr = toxURI . mid ( 4 ) ;
2014-11-06 22:12:10 +08:00
QString toxid = ToxDNS : : resolveToxAddress ( toxaddr , true ) . toString ( ) ;
if ( toxid . isEmpty ( ) )
{
QMessageBox : : warning ( 0 , " qTox " , toxaddr + " is not a valid Tox address. " ) ;
}
else
{
2015-02-24 21:10:51 +08:00
ToxURIDialog dialog ( 0 , toxaddr , QObject : : tr ( " %1 here! Tox me maybe? " , " Default message in Tox URI friend requests. Write something appropriate! " ) . arg ( Nexus : : getCore ( ) - > getUsername ( ) ) ) ;
2014-11-06 22:12:10 +08:00
if ( dialog . exec ( ) = = QDialog : : Accepted )
Core : : getInstance ( ) - > requestFriendship ( toxid , dialog . getRequestMessage ( ) ) ;
}
2015-02-20 20:26:41 +08:00
return true ;
2014-11-06 22:12:10 +08:00
}
ToxURIDialog : : ToxURIDialog ( QWidget * parent , const QString & userId , const QString & message ) :
QDialog ( parent )
{
setWindowFlags ( windowFlags ( ) & ~ Qt : : WindowContextHelpButtonHint ) ;
setWindowTitle ( tr ( " Add a friend " , " Title of the window to add a friend through Tox URI " ) ) ;
2014-11-29 19:27:36 +08:00
QLabel * friendsLabel = new QLabel ( tr ( " Do you want to add %1 as a friend? " ) . arg ( userId ) , this ) ;
2014-11-06 22:12:10 +08:00
QLabel * userIdLabel = new QLabel ( tr ( " User ID: " ) , this ) ;
QLineEdit * userIdEdit = new QLineEdit ( userId , this ) ;
userIdEdit - > setCursorPosition ( 0 ) ;
userIdEdit - > setReadOnly ( true ) ;
QLabel * messageLabel = new QLabel ( tr ( " Friend request message: " ) , this ) ;
messageEdit = new QPlainTextEdit ( message , this ) ;
QDialogButtonBox * buttonBox = new QDialogButtonBox ( Qt : : Horizontal , this ) ;
buttonBox - > addButton ( tr ( " Send " , " Send a friend request " ) , QDialogButtonBox : : AcceptRole ) ;
buttonBox - > addButton ( tr ( " Cancel " , " Don't send a friend request " ) , QDialogButtonBox : : RejectRole ) ;
connect ( buttonBox , & QDialogButtonBox : : accepted , this , & FriendRequestDialog : : accept ) ;
connect ( buttonBox , & QDialogButtonBox : : rejected , this , & FriendRequestDialog : : reject ) ;
QVBoxLayout * layout = new QVBoxLayout ( this ) ;
layout - > addWidget ( friendsLabel ) ;
layout - > addSpacing ( 12 ) ;
layout - > addWidget ( userIdLabel ) ;
layout - > addWidget ( userIdEdit ) ;
layout - > addWidget ( messageLabel ) ;
layout - > addWidget ( messageEdit ) ;
layout - > addWidget ( buttonBox ) ;
resize ( 300 , 200 ) ;
}
QString ToxURIDialog : : getRequestMessage ( )
{
return messageEdit - > toPlainText ( ) ;
}