2012-12-29 01:39:02 +00:00
/* profile_dialog.cpp
*
* Wireshark - Network traffic analyzer
* By Gerald Combs < gerald @ wireshark . org >
* Copyright 1998 Gerald Combs
*
2018-04-30 07:47:58 +00:00
* SPDX - License - Identifier : GPL - 2.0 - or - later
*/
2012-12-29 01:39:02 +00:00
# include "config.h"
# include <glib.h>
2013-11-17 02:55:14 +00:00
# include "wsutil/filesystem.h"
2019-07-21 10:03:25 +00:00
# include "wsutil/utf8_entities.h"
2012-12-29 01:39:02 +00:00
# include "epan/prefs.h"
2017-07-25 14:15:18 +00:00
# include <ui/qt/utils/qt_ui_utils.h>
2014-11-29 18:32:30 +00:00
2012-12-29 01:39:02 +00:00
# include "ui/profile.h"
2018-01-30 12:17:01 +00:00
# include "ui/recent.h"
2012-12-29 01:39:02 +00:00
2017-07-25 14:15:18 +00:00
# include <ui/qt/utils/variant_pointer.h>
2019-07-04 19:55:21 +00:00
# include <ui/qt/models/profile_model.h>
2017-01-11 12:55:23 +00:00
2012-12-29 01:39:02 +00:00
# include "profile_dialog.h"
2015-06-25 16:17:03 +00:00
# include <ui_profile_dialog.h>
2012-12-29 01:39:02 +00:00
# include "wireshark_application.h"
2017-07-25 14:15:18 +00:00
# include <ui/qt/utils/color_utils.h>
2019-07-10 22:25:45 +00:00
# include <ui/qt/simple_dialog.h>
2012-12-29 01:39:02 +00:00
# include <QBrush>
2013-07-25 23:49:47 +00:00
# include <QDir>
# include <QFont>
2012-12-29 01:39:02 +00:00
# include <QMessageBox>
2015-02-08 20:24:19 +00:00
# include <QPushButton>
# include <QTreeWidgetItem>
2013-07-25 23:49:47 +00:00
# include <QUrl>
2019-07-04 19:55:21 +00:00
# include <QComboBox>
# include <QLineEdit>
2019-07-10 22:25:45 +00:00
# include <QFileDialog>
# include <QStandardPaths>
2019-07-18 20:39:03 +00:00
# include <QKeyEvent>
2019-07-21 10:03:25 +00:00
# include <QMenu>
2019-07-24 21:39:25 +00:00
# include <QMessageBox>
# define PROFILE_EXPORT_PROPERTY "export"
# define PROFILE_EXPORT_ALL "all"
# define PROFILE_EXPORT_SELECTED "selected"
2012-12-29 01:39:02 +00:00
ProfileDialog : : ProfileDialog ( QWidget * parent ) :
2016-02-28 18:23:20 +00:00
GeometryStateDialog ( parent ) ,
2012-12-29 01:39:02 +00:00
pd_ui_ ( new Ui : : ProfileDialog ) ,
2019-07-04 19:55:21 +00:00
ok_button_ ( Q_NULLPTR ) ,
2019-07-22 19:27:39 +00:00
import_button_ ( Q_NULLPTR ) ,
2019-07-04 19:55:21 +00:00
model_ ( Q_NULLPTR ) ,
sort_model_ ( Q_NULLPTR )
2012-12-29 01:39:02 +00:00
{
pd_ui_ - > setupUi ( this ) ;
2016-02-28 18:23:20 +00:00
loadGeometry ( ) ;
2014-12-29 23:15:59 +00:00
setWindowTitle ( wsApp - > windowTitleString ( tr ( " Configuration Profiles " ) ) ) ;
2019-07-04 19:55:21 +00:00
2012-12-29 01:39:02 +00:00
ok_button_ = pd_ui_ - > buttonBox - > button ( QDialogButtonBox : : Ok ) ;
// XXX - Use NSImageNameAddTemplate and NSImageNameRemoveTemplate to set stock
2017-04-05 19:15:27 +00:00
// icons on macOS.
2012-12-29 01:39:02 +00:00
// Are there equivalent stock icons on Windows?
2019-06-20 18:25:49 +00:00
pd_ui_ - > newToolButton - > setStockIcon ( " list-add " ) ;
pd_ui_ - > deleteToolButton - > setStockIcon ( " list-remove " ) ;
pd_ui_ - > copyToolButton - > setStockIcon ( " list-copy " ) ;
2013-04-24 11:07:25 +00:00
# ifdef Q_OS_MAC
2012-12-29 01:39:02 +00:00
pd_ui_ - > newToolButton - > setAttribute ( Qt : : WA_MacSmallSize , true ) ;
pd_ui_ - > deleteToolButton - > setAttribute ( Qt : : WA_MacSmallSize , true ) ;
pd_ui_ - > copyToolButton - > setAttribute ( Qt : : WA_MacSmallSize , true ) ;
2019-07-21 12:56:07 +00:00
pd_ui_ - > lblInfo - > setAttribute ( Qt : : WA_MacSmallSize , true ) ;
2019-07-10 22:25:45 +00:00
# endif
2019-07-24 21:39:25 +00:00
import_button_ = pd_ui_ - > buttonBox - > addButton ( tr ( " Import " , " noun " ) , QDialogButtonBox : : ActionRole ) ;
2019-07-22 19:27:39 +00:00
2019-07-21 10:03:25 +00:00
# ifdef HAVE_MINIZIP
2019-07-24 21:39:25 +00:00
export_button_ = pd_ui_ - > buttonBox - > addButton ( tr ( " Export " , " noun " ) , QDialogButtonBox : : ActionRole ) ;
2019-07-22 19:27:39 +00:00
QMenu * importMenu = new QMenu ( import_button_ ) ;
2019-07-21 10:03:25 +00:00
QAction * entry = importMenu - > addAction ( tr ( UTF8_HORIZONTAL_ELLIPSIS " from Zip " ) ) ;
connect ( entry , & QAction : : triggered , this , & ProfileDialog : : importFromZip ) ;
entry = importMenu - > addAction ( tr ( UTF8_HORIZONTAL_ELLIPSIS " from Directory " ) ) ;
connect ( entry , & QAction : : triggered , this , & ProfileDialog : : importFromDirectory ) ;
2019-07-22 19:27:39 +00:00
import_button_ - > setMenu ( importMenu ) ;
2019-07-24 21:39:25 +00:00
QMenu * exportMenu = new QMenu ( export_button_ ) ;
export_selected_entry_ = exportMenu - > addAction ( tr ( UTF8_HORIZONTAL_ELLIPSIS " selected entry " ) ) ;
export_selected_entry_ - > setProperty ( PROFILE_EXPORT_PROPERTY , PROFILE_EXPORT_SELECTED ) ;
connect ( export_selected_entry_ , & QAction : : triggered , this , & ProfileDialog : : exportProfiles ) ;
entry = exportMenu - > addAction ( tr ( UTF8_HORIZONTAL_ELLIPSIS " all user profiles " ) ) ;
entry - > setProperty ( PROFILE_EXPORT_PROPERTY , PROFILE_EXPORT_ALL ) ;
connect ( entry , & QAction : : triggered , this , & ProfileDialog : : exportProfiles ) ;
export_button_ - > setMenu ( exportMenu ) ;
2019-07-21 10:03:25 +00:00
# else
2019-07-22 19:27:39 +00:00
connect ( import_button_ , & QPushButton : : clicked , this , & ProfileDialog : : importFromDirectory ) ;
2012-12-29 01:39:02 +00:00
# endif
2019-07-22 20:21:43 +00:00
resetTreeView ( ) ;
2015-11-22 14:25:27 +00:00
2019-07-04 19:55:21 +00:00
connect ( pd_ui_ - > profileTreeView , & ProfileTreeView : : currentItemChanged ,
this , & ProfileDialog : : currentItemChanged ) ;
2012-12-29 01:39:02 +00:00
2019-07-12 11:48:23 +00:00
connect ( pd_ui_ - > profileTreeView , & ProfileTreeView : : itemUpdated ,
this , & ProfileDialog : : editingFinished ) ;
2019-07-04 19:55:21 +00:00
/* Select the row for the currently selected profile or the first row if non is selected*/
selectProfile ( ) ;
QStringList items ;
2019-07-29 07:33:54 +00:00
items < < tr ( " All profiles " ) < < tr ( " System profiles " ) < < tr ( " User profiles " ) ;
2019-07-04 19:55:21 +00:00
pd_ui_ - > cmbProfileTypes - > addItems ( items ) ;
connect ( pd_ui_ - > cmbProfileTypes , SIGNAL ( currentTextChanged ( const QString & ) ) ,
this , SLOT ( filterChanged ( const QString & ) ) ) ;
connect ( pd_ui_ - > lineProfileFilter , SIGNAL ( textChanged ( const QString & ) ) ,
this , SLOT ( filterChanged ( const QString & ) ) ) ;
2019-07-12 19:35:05 +00:00
currentItemChanged ( ) ;
pd_ui_ - > profileTreeView - > setFocus ( ) ;
2012-12-29 01:39:02 +00:00
}
ProfileDialog : : ~ ProfileDialog ( )
{
delete pd_ui_ ;
empty_profile_list ( TRUE ) ;
}
2019-07-18 20:39:03 +00:00
void ProfileDialog : : keyPressEvent ( QKeyEvent * evt )
{
if ( pd_ui_ - > lineProfileFilter - > hasFocus ( ) & & ( evt - > key ( ) = = Qt : : Key_Enter | | evt - > key ( ) = = Qt : : Key_Return ) )
return ;
QDialog : : keyPressEvent ( evt ) ;
}
2019-07-04 19:55:21 +00:00
void ProfileDialog : : selectProfile ( QString profile )
{
if ( profile . isEmpty ( ) )
profile = QString ( get_profile_name ( ) ) ;
int row = model_ - > findByName ( profile ) ;
QModelIndex idx = sort_model_ - > mapFromSource ( model_ - > index ( row , ProfileModel : : COL_NAME ) ) ;
if ( idx . isValid ( ) )
pd_ui_ - > profileTreeView - > selectRow ( idx . row ( ) ) ;
}
2012-12-30 19:33:05 +00:00
int ProfileDialog : : execAction ( ProfileDialog : : ProfileAction profile_action )
{
int ret = QDialog : : Accepted ;
2019-07-04 19:55:21 +00:00
QModelIndex item ;
2012-12-30 19:33:05 +00:00
switch ( profile_action ) {
case ShowProfiles :
ret = exec ( ) ;
break ;
case NewProfile :
on_newToolButton_clicked ( ) ;
ret = exec ( ) ;
break ;
2019-07-21 10:03:25 +00:00
case ImportZipProfile :
2019-07-10 22:25:45 +00:00
# ifdef HAVE_MINIZIP
2019-07-21 10:03:25 +00:00
importFromZip ( ) ;
2019-07-10 22:25:45 +00:00
# endif
break ;
2019-07-21 10:03:25 +00:00
case ImportDirProfile :
importFromDirectory ( ) ;
break ;
2019-07-24 21:39:25 +00:00
case ExportSingleProfile :
# ifdef HAVE_MINIZIP
exportProfiles ( ) ;
# endif
break ;
case ExportAllProfiles :
# ifdef HAVE_MINIZIP
exportProfiles ( true ) ;
# endif
break ;
2012-12-30 19:33:05 +00:00
case EditCurrentProfile :
2019-07-04 19:55:21 +00:00
item = pd_ui_ - > profileTreeView - > currentIndex ( ) ;
if ( item . isValid ( ) ) {
pd_ui_ - > profileTreeView - > edit ( item ) ;
2012-12-30 19:33:05 +00:00
}
ret = exec ( ) ;
break ;
case DeleteCurrentProfile :
if ( delete_current_profile ( ) ) {
2019-07-04 19:55:21 +00:00
wsApp - > setConfigurationProfile ( Q_NULLPTR ) ;
2012-12-30 19:33:05 +00:00
}
break ;
}
return ret ;
}
2012-12-29 01:39:02 +00:00
void ProfileDialog : : updateWidgets ( )
{
bool enable_del = false ;
bool enable_ok = true ;
2019-07-22 20:21:43 +00:00
QString msg = " " ;
if ( model_ - > changesPending ( ) )
msg = tr ( " An import of profiles is not allowed, while changes are pending. " ) ;
2019-07-22 19:27:39 +00:00
import_button_ - > setToolTip ( msg ) ;
import_button_ - > setEnabled ( ! model_ - > changesPending ( ) ) ;
2019-07-22 15:03:46 +00:00
2019-07-04 19:55:21 +00:00
QModelIndex index = sort_model_ - > mapToSource ( pd_ui_ - > profileTreeView - > currentIndex ( ) ) ;
if ( index . column ( ) ! = ProfileModel : : COL_NAME )
index = index . sibling ( index . row ( ) , ProfileModel : : COL_NAME ) ;
if ( index . isValid ( ) ) {
2019-07-22 19:27:39 +00:00
if ( ! index . data ( ProfileModel : : DATA_IS_GLOBAL ) . toBool ( ) | | ! model_ - > resetDefault ( ) )
2012-12-29 01:39:02 +00:00
enable_del = true ;
}
2019-07-04 19:55:21 +00:00
if ( model_ & & model_ - > rowCount ( ) > 0 )
{
for ( int row = 0 ; row < model_ - > rowCount ( ) ; row + + )
{
QModelIndex idx = model_ - > index ( row , ProfileModel : : COL_NAME ) ;
QString name = idx . data ( ) . toString ( ) ;
2012-12-29 01:39:02 +00:00
2019-07-12 11:48:23 +00:00
if ( ! ProfileModel : : checkNameValidity ( name ) )
2019-07-04 19:55:21 +00:00
{
2017-02-07 20:35:16 +00:00
enable_ok = false ;
continue ;
}
2019-07-04 19:55:21 +00:00
if ( idx ! = index & & idx . data ( ) . toString ( ) . compare ( index . data ( ) . toString ( ) ) = = 0 )
{
if ( idx . data ( ProfileModel : : DATA_IS_GLOBAL ) . toBool ( ) = = index . data ( ProfileModel : : DATA_IS_GLOBAL ) . toBool ( ) )
enable_ok = false ;
2012-12-29 01:39:02 +00:00
}
2019-07-12 11:48:23 +00:00
QList < int > rows = model_ - > findAllByNameAndVisibility ( name , idx . data ( ProfileModel : : DATA_IS_GLOBAL ) . toBool ( ) ) ;
if ( rows . count ( ) > 1 )
enable_ok = false ;
2012-12-29 01:39:02 +00:00
}
}
2019-07-04 19:55:21 +00:00
pd_ui_ - > profileTreeView - > resizeColumnToContents ( 0 ) ;
2012-12-29 01:39:02 +00:00
pd_ui_ - > deleteToolButton - > setEnabled ( enable_del ) ;
ok_button_ - > setEnabled ( enable_ok ) ;
}
2019-07-04 19:55:21 +00:00
void ProfileDialog : : currentItemChanged ( )
2012-12-29 01:39:02 +00:00
{
2019-07-12 19:35:05 +00:00
QModelIndex idx = pd_ui_ - > profileTreeView - > currentIndex ( ) ;
if ( idx . isValid ( ) )
{
2019-07-20 21:38:24 +00:00
QString temp = idx . data ( ProfileModel : : DATA_PATH ) . toString ( ) ;
2019-07-12 19:35:05 +00:00
if ( idx . data ( ProfileModel : : DATA_PATH_IS_NOT_DESCRIPTION ) . toBool ( ) )
2019-07-20 21:38:24 +00:00
pd_ui_ - > lblInfo - > setUrl ( QUrl : : fromLocalFile ( temp ) . toString ( ) ) ;
2019-07-12 19:35:05 +00:00
else
pd_ui_ - > lblInfo - > setUrl ( QString ( ) ) ;
2019-07-20 21:38:24 +00:00
pd_ui_ - > lblInfo - > setText ( temp ) ;
pd_ui_ - > lblInfo - > setToolTip ( idx . data ( Qt : : ToolTipRole ) . toString ( ) ) ;
2019-07-12 19:35:05 +00:00
}
2019-07-04 19:55:21 +00:00
updateWidgets ( ) ;
2012-12-29 01:39:02 +00:00
}
void ProfileDialog : : on_newToolButton_clicked ( )
{
2019-07-22 19:27:39 +00:00
pd_ui_ - > lineProfileFilter - > setText ( " " ) ;
pd_ui_ - > cmbProfileTypes - > setCurrentIndex ( ProfileSortModel : : AllProfiles ) ;
2019-07-04 19:55:21 +00:00
sort_model_ - > setFilterString ( ) ;
QModelIndex ridx = sort_model_ - > mapFromSource ( model_ - > addNewProfile ( tr ( " New profile " ) ) ) ;
if ( ridx . isValid ( ) )
{
pd_ui_ - > profileTreeView - > setCurrentIndex ( ridx ) ;
2019-07-12 19:35:05 +00:00
pd_ui_ - > profileTreeView - > scrollTo ( ridx ) ;
2019-07-04 19:55:21 +00:00
pd_ui_ - > profileTreeView - > edit ( ridx ) ;
2019-07-12 19:35:05 +00:00
currentItemChanged ( ) ;
2019-07-04 19:55:21 +00:00
}
2019-07-22 15:03:46 +00:00
else
updateWidgets ( ) ;
2012-12-29 01:39:02 +00:00
}
void ProfileDialog : : on_deleteToolButton_clicked ( )
{
2019-07-04 19:55:21 +00:00
QModelIndex index = sort_model_ - > mapToSource ( pd_ui_ - > profileTreeView - > currentIndex ( ) ) ;
2012-12-29 01:39:02 +00:00
2019-07-04 19:55:21 +00:00
model_ - > deleteEntry ( index ) ;
2019-07-12 19:35:05 +00:00
2019-07-20 21:38:24 +00:00
QModelIndex newIdx = sort_model_ - > mapFromSource ( model_ - > index ( 0 , 0 ) ) ;
pd_ui_ - > profileTreeView - > setCurrentIndex ( newIdx ) ;
2019-07-22 15:03:46 +00:00
updateWidgets ( ) ;
2012-12-29 01:39:02 +00:00
}
void ProfileDialog : : on_copyToolButton_clicked ( )
{
2019-07-22 19:27:39 +00:00
pd_ui_ - > lineProfileFilter - > setText ( " " ) ;
2019-07-04 19:55:21 +00:00
pd_ui_ - > cmbProfileTypes - > setCurrentIndex ( ProfileSortModel : : AllProfiles ) ;
sort_model_ - > setFilterString ( ) ;
QModelIndex current = pd_ui_ - > profileTreeView - > currentIndex ( ) ;
if ( current . column ( ) ! = ProfileModel : : COL_NAME )
current = current . sibling ( current . row ( ) , ProfileModel : : COL_NAME ) ;
QModelIndex source = sort_model_ - > mapToSource ( current ) ;
QModelIndex ridx = model_ - > duplicateEntry ( source ) ;
if ( ridx . isValid ( ) )
{
pd_ui_ - > profileTreeView - > setCurrentIndex ( sort_model_ - > mapFromSource ( ridx ) ) ;
2019-07-12 19:35:05 +00:00
pd_ui_ - > profileTreeView - > scrollTo ( sort_model_ - > mapFromSource ( ridx ) ) ;
2019-07-04 19:55:21 +00:00
pd_ui_ - > profileTreeView - > edit ( sort_model_ - > mapFromSource ( ridx ) ) ;
2019-07-12 19:35:05 +00:00
currentItemChanged ( ) ;
2012-12-29 01:39:02 +00:00
}
2019-07-22 15:03:46 +00:00
else
updateWidgets ( ) ;
2012-12-29 01:39:02 +00:00
}
void ProfileDialog : : on_buttonBox_accepted ( )
{
2019-07-12 19:35:05 +00:00
bool write_recent = true ;
bool item_data_removed = false ;
2019-07-04 19:55:21 +00:00
QModelIndex index = sort_model_ - > mapToSource ( pd_ui_ - > profileTreeView - > currentIndex ( ) ) ;
2019-07-12 19:35:05 +00:00
QModelIndex default_item = sort_model_ - > mapFromSource ( model_ - > index ( 0 , ProfileModel : : COL_NAME ) ) ;
2019-07-04 19:55:21 +00:00
if ( index . column ( ) ! = ProfileModel : : COL_NAME )
index = index . sibling ( index . row ( ) , ProfileModel : : COL_NAME ) ;
if ( default_item . data ( ProfileModel : : DATA_STATUS ) . toInt ( ) = = PROF_STAT_DEFAULT & & model_ - > resetDefault ( ) )
{
2017-02-13 22:00:00 +00:00
// Reset Default profile.
2019-07-04 19:55:21 +00:00
GList * fl_entry = model_ - > at ( 0 ) ;
2017-02-13 22:00:00 +00:00
remove_from_profile_list ( fl_entry ) ;
// Don't write recent file if leaving the Default profile after this has been reset.
write_recent = ! is_default_profile ( ) ;
// Don't fetch profile data if removed.
2019-07-04 19:55:21 +00:00
item_data_removed = ( index . row ( ) = = 0 ) ;
2017-02-13 22:00:00 +00:00
}
2012-12-29 01:39:02 +00:00
2018-01-30 12:17:01 +00:00
if ( write_recent ) {
/* Get the current geometry, before writing it to disk */
wsApp - > emitAppSignal ( WiresharkApplication : : ProfileChanging ) ;
/* Write recent file for current profile now because
* the profile may be renamed in apply_profile_changes ( ) */
write_profile_recent ( ) ;
}
2019-07-04 19:55:21 +00:00
gchar * err_msg = Q_NULLPTR ;
if ( ( err_msg = apply_profile_changes ( ) ) ! = Q_NULLPTR ) {
2012-12-29 01:39:02 +00:00
QMessageBox : : critical ( this , tr ( " Profile Error " ) ,
err_msg ,
QMessageBox : : Ok ) ;
2018-01-18 07:33:03 +00:00
g_free ( err_msg ) ;
2019-07-04 19:55:21 +00:00
model_ - > doResetModel ( ) ;
2012-12-29 01:39:02 +00:00
return ;
}
2019-07-04 19:55:21 +00:00
model_ - > doResetModel ( ) ;
2019-07-12 19:35:05 +00:00
QString profileName ;
2019-07-04 19:55:21 +00:00
if ( index . isValid ( ) & & ! item_data_removed ) {
2019-07-12 19:35:05 +00:00
profileName = model_ - > data ( index ) . toString ( ) ;
2017-02-13 22:00:00 +00:00
}
2019-07-12 19:35:05 +00:00
if ( profileName . length ( ) > 0 & & model_ - > findByName ( profileName ) > = 0 ) {
2017-02-13 22:00:00 +00:00
// The new profile exists, change.
2019-07-12 19:35:05 +00:00
wsApp - > setConfigurationProfile ( profileName . toUtf8 ( ) . constData ( ) , FALSE ) ;
} else if ( ! model_ - > activeProfile ( ) . isValid ( ) ) {
2017-02-13 22:00:00 +00:00
// The new profile does not exist, and the previous profile has
// been deleted. Change to the default profile.
2019-07-04 19:55:21 +00:00
wsApp - > setConfigurationProfile ( Q_NULLPTR , FALSE ) ;
2012-12-29 01:42:01 +00:00
}
2012-12-29 01:39:02 +00:00
}
void ProfileDialog : : on_buttonBox_helpRequested ( )
{
wsApp - > helpTopicAction ( HELP_CONFIG_PROFILES_DIALOG ) ;
}
void ProfileDialog : : editingFinished ( )
{
2019-07-22 19:27:39 +00:00
pd_ui_ - > lineProfileFilter - > setText ( " " ) ;
pd_ui_ - > cmbProfileTypes - > setCurrentIndex ( ProfileSortModel : : AllProfiles ) ;
2019-07-12 19:35:05 +00:00
currentItemChanged ( ) ;
2012-12-29 01:39:02 +00:00
}
2019-07-04 19:55:21 +00:00
void ProfileDialog : : filterChanged ( const QString & text )
{
if ( qobject_cast < QComboBox * > ( sender ( ) ) )
{
QComboBox * cmb = qobject_cast < QComboBox * > ( sender ( ) ) ;
sort_model_ - > setFilterType ( static_cast < ProfileSortModel : : FilterType > ( cmb - > currentIndex ( ) ) ) ;
}
else if ( qobject_cast < QLineEdit * > ( sender ( ) ) )
sort_model_ - > setFilterString ( text ) ;
pd_ui_ - > profileTreeView - > resizeColumnToContents ( ProfileModel : : COL_NAME ) ;
QModelIndex active = sort_model_ - > mapFromSource ( model_ - > activeProfile ( ) ) ;
if ( active . isValid ( ) )
pd_ui_ - > profileTreeView - > setCurrentIndex ( active ) ;
}
2019-07-10 22:25:45 +00:00
# ifdef HAVE_MINIZIP
2019-07-24 21:39:25 +00:00
void ProfileDialog : : exportProfiles ( bool exportAll )
{
QAction * action = qobject_cast < QAction * > ( sender ( ) ) ;
if ( action & & action - > property ( PROFILE_EXPORT_PROPERTY ) . isValid ( ) )
exportAll = action - > property ( PROFILE_EXPORT_PROPERTY ) . toString ( ) . compare ( PROFILE_EXPORT_ALL ) = = 0 ;
QModelIndexList items ;
if ( ! exportAll & & pd_ui_ - > profileTreeView - > currentIndex ( ) . isValid ( ) )
items < < sort_model_ - > mapToSource ( pd_ui_ - > profileTreeView - > currentIndex ( ) ) ;
else if ( exportAll )
{
for ( int cnt = 0 ; cnt < sort_model_ - > rowCount ( ) ; cnt + + )
{
QModelIndex idx = sort_model_ - > index ( cnt , ProfileModel : : COL_NAME ) ;
if ( ! idx . data ( ProfileModel : : DATA_IS_GLOBAL ) . toBool ( ) & & ! idx . data ( ProfileModel : : DATA_IS_DEFAULT ) . toBool ( ) )
{
items < < sort_model_ - > mapToSource ( idx ) ;
}
}
}
if ( items . count ( ) = = 0 )
{
QMessageBox : : warning ( this , tr ( " Exporting profiles " ) , tr ( " No profiles found for export " ) ) ;
return ;
}
QString zipFile = QFileDialog : : getSaveFileName ( this , tr ( " Select zip file for export " ) , QString ( ) , tr ( " Zip File (*.zip) " ) ) ;
QString err ;
if ( model_ - > exportProfiles ( zipFile , items , & err ) )
2019-07-29 07:33:54 +00:00
QMessageBox : : information ( this , tr ( " Exporting profiles " ) , tr ( " %Ln profile(s) exported " , " " , items . count ( ) ) ) ;
2019-07-24 21:39:25 +00:00
else
QMessageBox : : warning ( this , tr ( " Exporting profiles " ) , QString ( " %1 \n \n %2: %3 " ) . arg ( tr ( " An error has occured while exporting profiles " ) ) . arg ( " Error " ) . arg ( err ) ) ;
}
2019-07-21 10:03:25 +00:00
void ProfileDialog : : importFromZip ( )
2019-07-10 22:25:45 +00:00
{
2019-07-19 09:46:10 +00:00
QString zipFile = QFileDialog : : getOpenFileName ( this , tr ( " Select zip file for import " ) , QString ( ) , tr ( " Zip File (*.zip) " ) ) ;
2019-07-10 22:25:45 +00:00
QFileInfo fi ( zipFile ) ;
if ( ! fi . exists ( ) )
return ;
2019-07-19 09:46:10 +00:00
int skipped = 0 ;
2019-07-21 10:03:25 +00:00
int count = model_ - > importProfilesFromZip ( zipFile , & skipped ) ;
2019-07-21 20:04:30 +00:00
QString msg ;
QMessageBox : : Icon icon ;
2019-07-19 09:46:10 +00:00
2019-07-21 20:04:30 +00:00
if ( count = = 0 & & skipped = = 0 )
{
icon = QMessageBox : : Warning ;
2019-07-21 10:03:25 +00:00
msg = tr ( " No profiles found for import in %1 " ) . arg ( fi . fileName ( ) ) ;
2019-07-21 20:04:30 +00:00
}
else
{
icon = QMessageBox : : Information ;
msg = tr ( " %Ln profile(s) imported " , " " , count ) ;
if ( skipped > 0 )
msg . append ( tr ( " , %Ln profile(s) skipped " , " " , skipped ) ) ;
}
2019-07-19 09:46:10 +00:00
2019-07-21 20:04:30 +00:00
QMessageBox msgBox ( icon , tr ( " Importing profiles " ) , msg , QMessageBox : : Ok , this ) ;
2019-07-21 10:03:25 +00:00
msgBox . exec ( ) ;
2019-07-22 20:21:43 +00:00
if ( count > 0 )
resetTreeView ( ) ;
2019-07-21 10:03:25 +00:00
}
# endif
2019-07-19 09:46:10 +00:00
2019-07-21 10:03:25 +00:00
void ProfileDialog : : importFromDirectory ( )
{
QString importDir = QFileDialog : : getExistingDirectory ( this , tr ( " Select directory for import " ) , QString ( ) ) ;
2019-07-19 09:46:10 +00:00
2019-07-21 10:03:25 +00:00
QFileInfo fi ( importDir ) ;
if ( ! fi . isDir ( ) )
return ;
int skipped = 0 ;
int count = model_ - > importProfilesFromDir ( importDir , & skipped ) ;
2019-07-21 20:04:30 +00:00
QString msg ;
QMessageBox : : Icon icon ;
2019-07-21 10:03:25 +00:00
2019-07-21 20:04:30 +00:00
if ( count = = 0 & & skipped = = 0 )
{
icon = QMessageBox : : Warning ;
2019-07-21 10:03:25 +00:00
msg = tr ( " No profiles found for import in %1 " ) . arg ( fi . fileName ( ) ) ;
2019-07-21 20:04:30 +00:00
}
else
{
icon = QMessageBox : : Information ;
msg = tr ( " %Ln profile(s) imported " , " " , count ) ;
if ( skipped > 0 )
msg . append ( tr ( " , %Ln profile(s) skipped " , " " , skipped ) ) ;
}
2019-07-21 10:03:25 +00:00
2019-07-21 20:04:30 +00:00
QMessageBox msgBox ( icon , tr ( " Importing profiles " ) , msg , QMessageBox : : Ok , this ) ;
2019-07-21 10:03:25 +00:00
msgBox . exec ( ) ;
2019-07-22 20:21:43 +00:00
if ( count > 0 )
resetTreeView ( ) ;
}
void ProfileDialog : : resetTreeView ( )
{
if ( model_ )
{
pd_ui_ - > profileTreeView - > setModel ( Q_NULLPTR ) ;
sort_model_ - > setSourceModel ( Q_NULLPTR ) ;
delete sort_model_ ;
delete model_ ;
}
model_ = new ProfileModel ( this ) ;
sort_model_ = new ProfileSortModel ( this ) ;
sort_model_ - > setSourceModel ( model_ ) ;
pd_ui_ - > profileTreeView - > setModel ( sort_model_ ) ;
if ( sort_model_ - > columnCount ( ) < = 1 )
pd_ui_ - > profileTreeView - > header ( ) - > hide ( ) ;
else
{
pd_ui_ - > profileTreeView - > header ( ) - > setStretchLastSection ( false ) ;
pd_ui_ - > profileTreeView - > header ( ) - > setSectionResizeMode ( ProfileModel : : COL_NAME , QHeaderView : : Stretch ) ;
}
2019-07-10 22:25:45 +00:00
}
2012-12-29 01:39:02 +00:00
/*
* Editor modelines
*
* Local Variables :
* c - basic - offset : 4
* tab - width : 8
* indent - tabs - mode : nil
* End :
*
* ex : set shiftwidth = 4 tabstop = 8 expandtab :
* : indentSize = 4 : tabSize = 8 : noTabs = true :
*/