2016-02-25 20:19:27 +00:00
// Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
// Copyright (C) 2010 Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
# include "stdpch.h"
2016-02-27 17:43:14 +00:00
# include "profilesdialog.h"
2016-03-05 11:38:28 +00:00
# include "profilesmodel.h"
2016-03-12 20:58:05 +00:00
# include "serversmodel.h"
2016-06-07 11:25:03 +00:00
# include "operationdialog.h"
2016-02-25 20:19:27 +00:00
# ifdef DEBUG_NEW
# define new DEBUG_NEW
# endif
2016-05-29 18:30:53 +00:00
CProfilesDialog : : CProfilesDialog ( QWidget * parent ) : QDialog ( parent ) , m_currentProfileIndex ( - 1 )
2016-02-25 20:19:27 +00:00
{
setupUi ( this ) ;
2016-03-01 13:00:06 +00:00
2016-03-05 11:38:28 +00:00
connect ( addButton , SIGNAL ( clicked ( ) ) , SLOT ( onAddProfile ( ) ) ) ;
connect ( deleteButton , SIGNAL ( clicked ( ) ) , SLOT ( onDeleteProfile ( ) ) ) ;
connect ( profilesListView , SIGNAL ( clicked ( QModelIndex ) ) , SLOT ( onProfileClicked ( QModelIndex ) ) ) ;
connect ( executableBrowseButton , SIGNAL ( clicked ( ) ) , SLOT ( onExecutableBrowseClicked ( ) ) ) ;
2016-06-11 15:00:09 +00:00
connect ( directoryButton , SIGNAL ( clicked ( ) ) , SLOT ( onProfileDirectoryClicked ( ) ) ) ;
2016-03-05 11:38:28 +00:00
m_model = new CProfilesModel ( this ) ;
2016-03-12 20:58:05 +00:00
m_serversModel = new CServersModel ( this ) ;
2016-03-05 11:38:28 +00:00
profilesListView - > setModel ( m_model ) ;
2016-03-12 20:58:05 +00:00
serverComboBox - > setModel ( m_serversModel ) ;
2016-03-05 11:38:28 +00:00
2016-05-16 12:46:35 +00:00
int index = CConfigFile : : getInstance ( ) - > getDefaultProfileIndex ( ) ;
2016-03-05 11:38:28 +00:00
2016-03-12 20:58:05 +00:00
profilesListView - > setCurrentIndex ( m_model - > index ( index , 0 ) ) ;
displayProfile ( index ) ;
2016-02-25 20:19:27 +00:00
}
2016-02-27 17:43:14 +00:00
CProfilesDialog : : ~ CProfilesDialog ( )
2016-02-25 20:19:27 +00:00
{
}
2016-02-27 17:43:14 +00:00
void CProfilesDialog : : accept ( )
2016-02-25 20:19:27 +00:00
{
2016-03-12 21:00:43 +00:00
saveProfile ( m_currentProfileIndex ) ;
2016-03-05 11:38:28 +00:00
m_model - > save ( ) ;
2016-02-25 20:19:27 +00:00
QDialog : : accept ( ) ;
}
2016-03-01 13:00:06 +00:00
void CProfilesDialog : : onAddProfile ( )
{
2016-06-11 14:59:21 +00:00
addProfile ( ) ;
2016-03-01 13:00:06 +00:00
}
void CProfilesDialog : : onDeleteProfile ( )
{
2016-03-05 11:38:28 +00:00
QMessageBox : : StandardButton res = QMessageBox : : question ( this , tr ( " Confirmation " ) , tr ( " You're going to delete a profile, files won't be deleted and you'll have to do that manually. \n Are you sure to delete this profile? " ) ) ;
if ( res ! = QMessageBox : : Yes ) return ;
QModelIndex index = profilesListView - > currentIndex ( ) ;
deleteProfile ( index . row ( ) ) ;
}
void CProfilesDialog : : onProfileClicked ( const QModelIndex & index )
{
qDebug ( ) < < " clicked on " < < index ;
displayProfile ( index . row ( ) ) ;
}
void CProfilesDialog : : displayProfile ( int index )
{
2016-03-12 20:59:29 +00:00
bool enabled = index > - 1 ;
profileIdLabel - > setEnabled ( enabled ) ;
nameEdit - > setEnabled ( enabled ) ;
serverComboBox - > setEnabled ( enabled ) ;
argumentsEdit - > setEnabled ( enabled ) ;
commentsEdit - > setEnabled ( enabled ) ;
2016-03-05 11:38:28 +00:00
if ( index < 0 ) return ;
saveProfile ( m_currentProfileIndex ) ;
const CProfile & profile = m_model - > getProfiles ( ) [ index ] ;
2016-06-11 15:00:09 +00:00
QString executable = profile . executable ;
if ( executable . isEmpty ( ) )
{
executable = CConfigFile : : getInstance ( ) - > getServerClientFullPath ( profile . server ) ;
}
2016-06-14 17:45:29 +00:00
QString profileDirectory = profile . getDirectory ( ) ;
2016-06-11 15:00:09 +00:00
2016-03-12 21:01:03 +00:00
// update all widgets with content of profile
2016-05-16 12:45:43 +00:00
profileIdLabel - > setText ( profile . id ) ;
2016-03-05 11:38:28 +00:00
nameEdit - > setText ( profile . name ) ;
2016-03-12 21:00:12 +00:00
serverComboBox - > setCurrentIndex ( m_serversModel - > getIndexFromServerID ( profile . server ) ) ;
2016-06-11 15:02:44 +00:00
executablePathLabel - > setText ( QFileInfo ( executable ) . fileName ( ) ) ;
2016-03-05 11:38:28 +00:00
argumentsEdit - > setText ( profile . arguments ) ;
commentsEdit - > setPlainText ( profile . comments ) ;
2016-06-11 15:00:09 +00:00
directoryPathLabel - > setText ( profileDirectory ) ;
2016-03-12 20:58:53 +00:00
desktopShortcutCheckBox - > setChecked ( profile . desktopShortcut ) ;
menuShortcutCheckBox - > setChecked ( profile . menuShortcut ) ;
2016-06-11 15:00:09 +00:00
// disable click on button if directory doesn't exist
directoryButton - > setEnabled ( QFile : : exists ( profileDirectory ) ) ;
2016-03-12 20:58:53 +00:00
updateExecutableVersion ( index ) ;
2016-03-05 11:38:28 +00:00
m_currentProfileIndex = index ;
}
void CProfilesDialog : : saveProfile ( int index )
{
2016-06-11 15:03:14 +00:00
if ( index < 0 | | index > = m_model - > rowCount ( ) ) return ;
2016-03-05 11:38:28 +00:00
CProfile & profile = m_model - > getProfiles ( ) [ index ] ;
2016-03-12 21:00:12 +00:00
2016-03-05 11:38:28 +00:00
profile . name = nameEdit - > text ( ) ;
2016-03-12 21:00:12 +00:00
profile . server = m_serversModel - > getServerIDFromIndex ( serverComboBox - > currentIndex ( ) ) ;
2016-03-05 11:38:28 +00:00
profile . arguments = argumentsEdit - > text ( ) ;
profile . comments = commentsEdit - > toPlainText ( ) ;
2016-03-12 21:01:03 +00:00
profile . desktopShortcut = desktopShortcutCheckBox - > isChecked ( ) ;
profile . menuShortcut = menuShortcutCheckBox - > isChecked ( ) ;
2016-03-05 11:38:28 +00:00
}
void CProfilesDialog : : deleteProfile ( int index )
{
if ( index < 0 ) return ;
m_model - > removeRow ( index ) ;
2016-06-07 11:25:03 +00:00
2016-06-11 15:03:14 +00:00
// decrement profile index
- - index ;
// select row and update content
profilesListView - > setCurrentIndex ( m_model - > index ( index , 0 ) ) ;
displayProfile ( index ) ;
// TODO: delete files for delete profile
2016-03-05 11:38:28 +00:00
}
void CProfilesDialog : : addProfile ( )
{
2016-06-11 14:59:21 +00:00
int index = m_model - > rowCount ( ) ;
// append the new profile
m_model - > insertRow ( index ) ;
CConfigFile * config = CConfigFile : : getInstance ( ) ;
CProfile & profile = m_model - > getProfiles ( ) [ index ] ;
const CServer & server = config - > getServer ( config - > getDefaultServerIndex ( ) ) ;
int nextId = 0 ;
// search an ID that doesn't correspond to an existing profile directory
while ( QFile : : exists ( config - > getProfileDirectory ( ) + " / " + QString : : number ( nextId ) ) ) + + nextId ;
// increment this ID until not used in profiles
while ( nextId < 100 )
{
bool found = false ;
// search if this ID is already used in existing profiles
foreach ( const CProfile & p , m_model - > getProfiles ( ) )
{
if ( p . id = = QString : : number ( nextId ) )
{
found = true ;
break ;
}
}
if ( ! found ) break ;
// increment ID
+ + nextId ;
}
// set default parameters
profile . id = QString : : number ( nextId ) ;
profile . server = server . id ;
profilesListView - > setCurrentIndex ( m_model - > index ( index , 0 ) ) ;
displayProfile ( index ) ;
// TODO: copy files to new server if files don't exist
2016-03-05 11:38:28 +00:00
}
2016-03-12 20:58:53 +00:00
void CProfilesDialog : : updateExecutableVersion ( int index )
2016-03-05 11:38:28 +00:00
{
2016-03-12 20:58:53 +00:00
if ( index < 0 ) return ;
2016-03-05 11:38:28 +00:00
2016-03-12 20:58:53 +00:00
const CProfile & profile = m_model - > getProfiles ( ) [ index ] ;
2016-03-05 11:38:28 +00:00
2016-03-12 20:58:53 +00:00
QString executable = profile . executable ;
2016-03-05 11:38:28 +00:00
2016-03-12 20:58:53 +00:00
// file empty, use default one
if ( executable . isEmpty ( ) )
{
2016-06-11 15:02:44 +00:00
executable + = CConfigFile : : getInstance ( ) - > getServerClientFullPath ( profile . server ) ;
2016-03-12 20:58:53 +00:00
}
2016-03-05 11:38:28 +00:00
2016-03-12 20:58:53 +00:00
// file doesn't exist
2016-06-11 15:02:44 +00:00
if ( executable . isEmpty ( ) | | ! QFile : : exists ( executable ) ) return ;
2016-03-05 11:38:28 +00:00
2016-03-12 20:58:53 +00:00
// launch executable with --version argument
2016-03-05 11:38:28 +00:00
QProcess process ;
process . setProcessChannelMode ( QProcess : : MergedChannels ) ;
2016-03-12 20:58:53 +00:00
process . start ( executable , QStringList ( ) < < " --version " , QIODevice : : ReadWrite ) ;
2016-03-05 11:38:28 +00:00
if ( ! process . waitForStarted ( ) ) return ;
QByteArray data ;
2016-03-12 20:58:53 +00:00
// read all output
2016-03-05 11:38:28 +00:00
while ( process . waitForReadyRead ( ) ) data . append ( process . readAll ( ) ) ;
2016-03-12 20:58:53 +00:00
// convert output to string
2016-03-05 11:38:28 +00:00
QString versionString = QString : : fromUtf8 ( data ) ;
2016-03-12 20:58:53 +00:00
// parse version from output
2016-03-05 11:38:28 +00:00
QRegExp reg ( " ([A-Za-z0-1_.]+) ( ( DEV | FV ) ( [ 0 - 9. ] + ) ) " ) ;
if ( reg . indexIn ( versionString ) > - 1 )
{
executableVersionLabel - > setText ( reg . cap ( 2 ) ) ;
}
2016-03-12 20:58:53 +00:00
}
void CProfilesDialog : : onExecutableBrowseClicked ( )
{
if ( m_currentProfileIndex < 0 ) return ;
CProfile & profile = m_model - > getProfiles ( ) [ m_currentProfileIndex ] ;
2016-06-11 15:02:44 +00:00
QString executable = profile . executable ;
if ( executable . isEmpty ( ) )
{
executable = CConfigFile : : getInstance ( ) - > getServerClientFullPath ( profile . server ) ;
}
executable = QFileDialog : : getOpenFileName ( this , tr ( " Please choose Ryzom client executable to launch " ) , executable , tr ( " Executables (*.exe) " ) ) ;
2016-03-12 20:58:53 +00:00
2016-06-11 15:02:44 +00:00
if ( executable . isEmpty ( ) ) return ;
2016-03-12 20:58:53 +00:00
2016-06-11 15:02:44 +00:00
// don't need to save the new executable if the same as default one
if ( executable = = CConfigFile : : getInstance ( ) - > getServerClientFullPath ( profile . server ) )
{
profile . executable . clear ( ) ;
}
else
{
profile . executable = executable ;
}
2016-03-12 20:58:53 +00:00
2016-06-11 15:02:44 +00:00
executablePathLabel - > setText ( QFileInfo ( executable ) . fileName ( ) ) ;
2016-03-05 11:38:28 +00:00
2016-03-12 20:58:53 +00:00
updateExecutableVersion ( m_currentProfileIndex ) ;
2016-03-01 13:00:06 +00:00
}
2016-06-11 15:00:09 +00:00
void CProfilesDialog : : onProfileDirectoryClicked ( )
{
if ( m_currentProfileIndex < 0 ) return ;
const CProfile & profile = m_model - > getProfiles ( ) [ m_currentProfileIndex ] ;
2016-06-14 17:45:29 +00:00
QString profileDirectory = profile . getDirectory ( ) ;
2016-06-11 15:00:09 +00:00
QDesktopServices : : openUrl ( QUrl : : fromLocalFile ( profileDirectory ) ) ;
}