2012-08-13 11:37:21 -04:00
/****************************************************************
* RetroShare is distributed under the following license :
*
* Copyright ( C ) 2012 , RetroShare Team
*
* This program is free 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 2
* 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
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
2012-09-12 07:17:00 -04:00
* Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
2012-08-13 11:37:21 -04:00
* Boston , MA 02110 - 1301 , USA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include <rshare.h>
# include <util/rsrandom.h>
# include <retroshare/rsinit.h>
# include <retroshare/rspeers.h>
# include "ProfileManager.h"
# include "gui/GenCertDialog.h"
2012-08-13 18:53:55 -04:00
# include "gui/common/RSTreeWidgetItem.h"
2012-08-13 11:37:21 -04:00
# include <QAbstractEventDispatcher>
# include <QFileDialog>
# include <QMessageBox>
# include <QTreeWidget>
2012-08-13 18:53:55 -04:00
# include <QMenu>
2012-08-13 11:37:21 -04:00
# include <time.h>
2012-08-13 18:53:55 -04:00
# define IMAGE_EXPORT ": / images / exportpeers_16x16.png"
2012-08-13 11:37:21 -04:00
2012-09-12 07:17:00 -04:00
# define COLUMN_NAME 0
# define COLUMN_EMAIL 1
# define COLUMN_GID 2
2012-08-13 11:37:21 -04:00
/** Default constructor */
2012-11-06 18:26:47 -05:00
ProfileManager : : ProfileManager ( QWidget * parent )
: QDialog ( parent , Qt : : WindowSystemMenuHint | Qt : : WindowTitleHint | Qt : : WindowCloseButtonHint )
2012-08-13 11:37:21 -04:00
{
2012-09-12 07:17:00 -04:00
/* Invoke Qt Designer generated QObject setup routine */
ui . setupUi ( this ) ;
2012-08-13 11:37:21 -04:00
2012-09-12 07:17:00 -04:00
ui . headerFrame - > setHeaderImage ( QPixmap ( " :/images/contact_new128.png " ) ) ;
ui . headerFrame - > setHeaderText ( tr ( " Profile Manager " ) ) ;
2012-08-13 11:37:21 -04:00
2012-09-12 07:17:00 -04:00
connect ( ui . identityTreeWidget , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( identityTreeWidgetCostumPopupMenu ( QPoint ) ) ) ;
connect ( ui . identityTreeWidget , SIGNAL ( currentItemChanged ( QTreeWidgetItem * , QTreeWidgetItem * ) ) , this , SLOT ( identityItemChanged ( QTreeWidgetItem * , QTreeWidgetItem * ) ) ) ;
connect ( ui . exportIdentity_PB , SIGNAL ( clicked ( ) ) , this , SLOT ( exportIdentity ( ) ) ) ;
2012-08-13 11:37:21 -04:00
2014-09-25 16:14:21 -04:00
// ui.exportIdentity_PB->setEnabled(false);
2012-09-12 07:17:00 -04:00
fillIdentities ( ) ;
2012-08-13 11:37:21 -04:00
}
2012-09-12 07:17:00 -04:00
void ProfileManager : : identityTreeWidgetCostumPopupMenu ( QPoint )
2012-08-13 18:53:55 -04:00
{
2012-09-12 07:17:00 -04:00
QTreeWidgetItem * item = getCurrentIdentity ( ) ;
2012-08-13 18:53:55 -04:00
2012-09-12 07:17:00 -04:00
QMenu contextMnu ( this ) ;
2012-08-13 18:53:55 -04:00
2012-09-12 07:17:00 -04:00
QAction * action = contextMnu . addAction ( QIcon ( IMAGE_EXPORT ) , tr ( " Export Identity " ) , this , SLOT ( exportIdentity ( ) ) ) ;
action - > setEnabled ( item ! = NULL ) ;
2012-08-13 18:53:55 -04:00
contextMnu . exec ( QCursor : : pos ( ) ) ;
}
2012-09-12 07:17:00 -04:00
void ProfileManager : : identityItemChanged ( QTreeWidgetItem * current , QTreeWidgetItem */ * previous */ )
2012-08-13 11:37:21 -04:00
{
2012-09-12 07:17:00 -04:00
ui . exportIdentity_PB - > setEnabled ( current ! = NULL ) ;
}
2012-08-13 18:53:55 -04:00
2012-09-12 07:17:00 -04:00
void ProfileManager : : fillIdentities ( )
{
ui . identityTreeWidget - > clear ( ) ;
ui . identityTreeWidget - > setColumnWidth ( COLUMN_NAME , 200 ) ;
ui . identityTreeWidget - > setColumnWidth ( COLUMN_EMAIL , 200 ) ;
2014-09-25 16:14:21 -04:00
RsPgpId own_pgp_id = rsPeers - > getGPGOwnId ( ) ;
2012-09-12 07:17:00 -04:00
std : : cerr < < " Finding PGPUsers " < < std : : endl ;
QTreeWidget * identityTreeWidget = ui . identityTreeWidget ;
QTreeWidgetItem * item ;
2014-03-17 16:56:06 -04:00
std : : list < RsPgpId > pgpIds ;
std : : list < RsPgpId > : : iterator it ;
2012-08-13 11:37:21 -04:00
2014-01-17 21:35:06 -05:00
if ( RsAccounts : : GetPGPLogins ( pgpIds ) ) {
2014-10-21 18:33:02 -04:00
for ( it = pgpIds . begin ( ) ; it ! = pgpIds . end ( ) ; + + it ) {
2012-09-12 07:17:00 -04:00
std : : string name , email ;
2014-01-17 21:35:06 -05:00
RsAccounts : : GetPGPLoginDetails ( * it , name , email ) ;
2012-09-12 07:17:00 -04:00
std : : cerr < < " Adding PGPUser: " < < name < < " id: " < < * it < < std : : endl ;
2014-03-17 16:56:06 -04:00
QString gid = QString : : fromStdString ( ( * it ) . toStdString ( ) ) ;
2012-08-13 11:37:21 -04:00
2012-09-12 07:17:00 -04:00
item = new RSTreeWidgetItem ( NULL , 0 ) ;
item - > setText ( COLUMN_NAME , QString : : fromUtf8 ( name . c_str ( ) ) ) ;
item - > setText ( COLUMN_EMAIL , QString : : fromUtf8 ( email . c_str ( ) ) ) ;
item - > setText ( COLUMN_GID , gid ) ;
identityTreeWidget - > addTopLevelItem ( item ) ;
2014-09-25 16:14:21 -04:00
if ( own_pgp_id = = * it )
{
item - > setSelected ( true ) ;
ui . exportIdentity_PB - > setEnabled ( true ) ;
}
2012-09-12 07:17:00 -04:00
}
}
// for (int i = 0; i < ui.identityTreeWidget->columnCount(); ++i) {
// ui.identityTreeWidget->resizeColumnToContents(i);
// }
2012-08-13 11:37:21 -04:00
}
void ProfileManager : : exportIdentity ( )
{
2012-09-12 07:17:00 -04:00
QTreeWidgetItem * item = getCurrentIdentity ( ) ;
if ( ! item )
return ;
2012-08-13 11:37:21 -04:00
2014-03-17 16:56:06 -04:00
RsPgpId gpgId ( item - > text ( COLUMN_GID ) . toStdString ( ) ) ;
if ( gpgId . isNull ( ) )
2012-09-12 07:17:00 -04:00
return ;
2012-08-13 11:37:21 -04:00
2012-09-12 07:17:00 -04:00
QString fname = QFileDialog : : getSaveFileName ( this , tr ( " Export Identity " ) , " " , tr ( " RetroShare Identity files (*.asc) " ) ) ;
if ( fname . isNull ( ) )
return ;
if ( QFileInfo ( fname ) . suffix ( ) . isEmpty ( ) ) {
fname + = " .asc " ;
}
2012-08-13 11:37:21 -04:00
2014-01-17 21:35:06 -05:00
if ( RsAccounts : : ExportIdentity ( fname . toUtf8 ( ) . constData ( ) , gpgId ) )
2012-09-12 07:17:00 -04:00
QMessageBox : : information ( this , tr ( " Identity saved " ) , tr ( " Your identity was successfully saved \n It is encrypted \n \n You can now copy it to another computer \n and use the import button to load it " ) ) ;
2012-08-13 11:37:21 -04:00
else
2012-10-11 06:28:24 -04:00
QMessageBox : : information ( this , tr ( " Identity not saved " ) , tr ( " Your identity was not saved. An error occurred. " ) ) ;
2012-08-13 11:37:21 -04:00
}
2012-08-13 18:53:55 -04:00
2012-08-13 11:37:21 -04:00
void ProfileManager : : importIdentity ( )
{
2012-10-01 16:21:06 -04:00
QString fname = QFileDialog : : getOpenFileName ( this , tr ( " Import Identity " ) , " " , tr ( " RetroShare Identity files (*.asc) " ) ) ;
2012-08-13 11:37:21 -04:00
if ( fname . isNull ( ) )
return ;
2014-03-17 16:56:06 -04:00
RsPgpId gpg_id ;
2012-08-13 11:37:21 -04:00
std : : string err_string ;
2014-01-17 21:35:06 -05:00
if ( ! RsAccounts : : ImportIdentity ( fname . toUtf8 ( ) . constData ( ) , gpg_id , err_string ) )
2012-08-13 11:37:21 -04:00
{
QMessageBox : : information ( this , tr ( " Identity not loaded " ) , tr ( " Your identity was not loaded properly: " ) + " \n " + QString : : fromStdString ( err_string ) ) ;
return ;
}
else
{
std : : string name , email ;
2014-01-17 21:35:06 -05:00
RsAccounts : : GetPGPLoginDetails ( gpg_id , name , email ) ;
2012-08-13 11:37:21 -04:00
std : : cerr < < " Adding PGPUser: " < < name < < " id: " < < gpg_id < < std : : endl ;
2014-07-07 15:37:04 -04:00
QMessageBox : : information ( this , tr ( " New identity imported " ) , tr ( " Your identity was imported successfully: " ) + " \n " + " \n Name : " + QString : : fromUtf8 ( name . c_str ( ) ) + " \n email: " + QString : : fromStdString ( email ) + " \n Key ID: " + QString : : fromStdString ( gpg_id . toStdString ( ) ) + " \n \n " + tr ( " You can use it now to create a new location. " ) ) ;
2012-08-13 11:37:21 -04:00
}
2012-09-12 07:17:00 -04:00
fillIdentities ( ) ;
2012-08-13 11:37:21 -04:00
}
void ProfileManager : : selectFriend ( )
{
#if 0
/* still need to find home (first) */
QString fileName = QFileDialog : : getOpenFileName ( this , tr ( " Select Trusted Friend " ) , " " ,
2012-09-12 07:17:00 -04:00
tr ( " Certificates (*.pqi *.pem) " ) ) ;
2012-08-13 11:37:21 -04:00
std : : string fname , userName ;
fname = fileName . toStdString ( ) ;
if ( RsInit : : ValidateTrustedUser ( fname , userName ) )
{
ui . genFriend - > setText ( QString : : fromStdString ( userName ) ) ;
}
else
{
ui . genFriend - > setText ( " <Invalid Selected> " ) ;
}
# endif
}
void ProfileManager : : checkChanged ( int /*i*/ )
{
#if 0
if ( i )
{
selectFriend ( ) ;
}
else
{
/* invalidate selection */
std : : string fname = " " ;
std : : string userName = " " ;
RsInit : : ValidateTrustedUser ( fname , userName ) ;
ui . genFriend - > setText ( " <None Selected> " ) ;
}
# endif
}
void ProfileManager : : newIdentity ( )
{
2012-10-29 07:02:03 -04:00
GenCertDialog gd ( true ) ;
2012-09-12 07:17:00 -04:00
gd . exec ( ) ;
fillIdentities ( ) ;
2012-08-13 11:37:21 -04:00
}
2012-08-13 18:53:55 -04:00
QTreeWidgetItem * ProfileManager : : getCurrentIdentity ( )
{
2012-09-12 07:17:00 -04:00
if ( ui . identityTreeWidget - > selectedItems ( ) . size ( ) ! = 0 ) {
return ui . identityTreeWidget - > currentItem ( ) ;
}
2012-08-13 18:53:55 -04:00
2012-09-12 07:17:00 -04:00
return NULL ;
2012-08-13 18:53:55 -04:00
}