2017-03-12 23:26:04 +01:00
/*
KLayout Layout Viewer
Copyright ( C ) 2006 - 2017 Matthias Koefferlein
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
Foundation , Inc . , 51 Franklin St , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
# include "laySaltManagerDialog.h"
2017-03-25 00:08:17 +01:00
# include "laySaltModel.h"
2017-03-19 22:44:46 +01:00
# include "laySaltGrainPropertiesDialog.h"
2017-03-27 15:46:01 +02:00
# include "laySaltDownloadManager.h"
2017-03-18 00:22:45 +01:00
# include "laySalt.h"
2017-04-22 21:45:49 +02:00
# include "layVersion.h"
2017-03-23 22:19:13 +01:00
# include "ui_SaltGrainTemplateSelectionDialog.h"
2017-03-18 00:22:45 +01:00
# include "tlString.h"
2017-03-23 23:46:25 +01:00
# include "tlExceptions.h"
2017-03-18 00:22:45 +01:00
# include <QTextDocument>
# include <QPainter>
# include <QDir>
2017-03-18 22:36:33 +01:00
# include <QTextStream>
# include <QBuffer>
2017-03-23 22:19:13 +01:00
# include <QResource>
2017-03-25 00:08:17 +01:00
# include <QMessageBox>
# include <QAbstractItemModel>
# include <QStyledItemDelegate>
2017-03-12 23:26:04 +01:00
namespace lay
{
2017-03-18 22:36:33 +01:00
// --------------------------------------------------------------------------------------
2017-03-23 22:19:13 +01:00
/**
* @ brief A tiny dialog to select a template and a name for the grain
*/
class SaltGrainTemplateSelectionDialog
: public QDialog , private Ui : : SaltGrainTemplateSelectionDialog
{
public :
2017-03-23 23:46:25 +01:00
SaltGrainTemplateSelectionDialog ( QWidget * parent , lay : : Salt * salt )
: QDialog ( parent ) , mp_salt ( salt )
2017-03-23 22:19:13 +01:00
{
Ui : : SaltGrainTemplateSelectionDialog : : setupUi ( this ) ;
m_salt_templates . add_location ( " :/salt_templates " ) ;
salt_view - > setModel ( new SaltModel ( this , & m_salt_templates ) ) ;
salt_view - > setItemDelegate ( new SaltItemDelegate ( this ) ) ;
salt_view - > setCurrentIndex ( salt_view - > model ( ) - > index ( 0 , 0 , QModelIndex ( ) ) ) ;
}
lay : : SaltGrain templ ( ) const
{
SaltModel * model = dynamic_cast < SaltModel * > ( salt_view - > model ( ) ) ;
tl_assert ( model ! = 0 ) ;
SaltGrain * g = model - > grain_from_index ( salt_view - > currentIndex ( ) ) ;
tl_assert ( g ! = 0 ) ;
return * g ;
}
2017-03-23 23:46:25 +01:00
std : : string name ( ) const
{
return tl : : to_string ( name_edit - > text ( ) ) ;
}
2017-03-23 22:19:13 +01:00
void accept ( )
{
name_alert - > clear ( ) ;
std : : string name = tl : : to_string ( name_edit - > text ( ) . simplified ( ) ) ;
if ( name . empty ( ) ) {
name_alert - > error ( ) < < tr ( " Name must not be empty " ) ;
} else if ( ! SaltGrain : : valid_name ( name ) ) {
name_alert - > error ( ) < < tr ( " Name is not valid (must be composed of letters, digits or underscores. \n Groups and names need to be separated with slashes. " ) ;
} else {
2017-03-23 23:46:25 +01:00
// check, if this name does not exist yet
for ( Salt : : flat_iterator g = mp_salt - > begin_flat ( ) ; g ! = mp_salt - > end_flat ( ) ; + + g ) {
if ( ( * g ) - > name ( ) = = name ) {
name_alert - > error ( ) < < tr ( " A package with this name already exists " ) ;
return ;
}
}
2017-03-23 22:19:13 +01:00
QDialog : : accept ( ) ;
2017-03-23 23:46:25 +01:00
2017-03-23 22:19:13 +01:00
}
}
private :
lay : : Salt m_salt_templates ;
2017-03-23 23:46:25 +01:00
lay : : Salt * mp_salt ;
2017-03-23 22:19:13 +01:00
} ;
2017-03-18 22:36:33 +01:00
// --------------------------------------------------------------------------------------
// SaltManager implementation
2017-04-22 18:09:12 +02:00
SaltManagerDialog : : SaltManagerDialog ( QWidget * parent , lay : : Salt * salt , const std : : string & salt_mine_url )
2017-03-18 22:36:33 +01:00
: QDialog ( parent ) ,
2017-04-22 18:09:12 +02:00
m_salt_mine_url ( salt_mine_url ) ,
dm_update_models ( this , & SaltManagerDialog : : update_models ) , m_current_tab ( - 1 )
2017-03-12 23:26:04 +01:00
{
Ui : : SaltManagerDialog : : setupUi ( this ) ;
2017-03-19 22:44:46 +01:00
mp_properties_dialog = new lay : : SaltGrainPropertiesDialog ( this ) ;
connect ( edit_button , SIGNAL ( clicked ( ) ) , this , SLOT ( edit_properties ( ) ) ) ;
2017-03-23 22:19:13 +01:00
connect ( create_button , SIGNAL ( clicked ( ) ) , this , SLOT ( create_grain ( ) ) ) ;
connect ( delete_button , SIGNAL ( clicked ( ) ) , this , SLOT ( delete_grain ( ) ) ) ;
2017-04-22 18:09:12 +02:00
connect ( apply_new_button , SIGNAL ( clicked ( ) ) , this , SLOT ( apply ( ) ) ) ;
connect ( apply_update_button , SIGNAL ( clicked ( ) ) , this , SLOT ( apply ( ) ) ) ;
2017-03-12 23:26:04 +01:00
2017-04-16 23:03:33 +02:00
mp_salt = salt ;
2017-04-22 18:09:12 +02:00
try {
if ( ! m_salt_mine_url . empty ( ) ) {
tl : : log < < tl : : to_string ( tr ( " Downloading package repository from %1 " ) . arg ( tl : : to_qstring ( m_salt_mine_url ) ) ) ;
m_salt_mine . load ( m_salt_mine_url ) ;
}
} catch ( tl : : Exception & ex ) {
tl : : error < < ex . msg ( ) ;
}
2017-03-25 10:15:24 +01:00
2017-03-26 01:02:40 +01:00
SaltModel * model = new SaltModel ( this , mp_salt ) ;
2017-03-18 22:36:33 +01:00
salt_view - > setModel ( model ) ;
2017-03-18 00:22:45 +01:00
salt_view - > setItemDelegate ( new SaltItemDelegate ( this ) ) ;
2017-04-22 18:09:12 +02:00
SaltModel * mine_model ;
// This model will show only the grains of mp_salt_mine which are not present in mp_salt yet.
mine_model = new SaltModel ( this , & m_salt_mine , mp_salt , true ) ;
salt_mine_view_new - > setModel ( mine_model ) ;
salt_mine_view_new - > setItemDelegate ( new SaltItemDelegate ( this ) ) ;
// This model will show only the grains of mp_salt_mine which are present in mp_salt already.
mine_model = new SaltModel ( this , & m_salt_mine , mp_salt , false ) ;
salt_mine_view_update - > setModel ( mine_model ) ;
salt_mine_view_update - > setItemDelegate ( new SaltItemDelegate ( this ) ) ;
2017-03-25 10:15:24 +01:00
mode_tab - > setCurrentIndex ( mp_salt - > is_empty ( ) ? 1 : 0 ) ;
connect ( mode_tab , SIGNAL ( currentChanged ( int ) ) , this , SLOT ( mode_changed ( ) ) ) ;
2017-04-22 18:09:12 +02:00
m_current_tab = mode_tab - > currentIndex ( ) ;
2017-03-26 01:02:40 +01:00
2017-03-18 22:36:33 +01:00
connect ( mp_salt , SIGNAL ( collections_changed ( ) ) , this , SLOT ( salt_changed ( ) ) ) ;
2017-04-20 00:09:53 +02:00
connect ( mp_salt , SIGNAL ( collections_about_to_change ( ) ) , this , SLOT ( salt_about_to_change ( ) ) ) ;
2017-04-22 18:09:12 +02:00
connect ( & m_salt_mine , SIGNAL ( collections_changed ( ) ) , this , SLOT ( salt_mine_changed ( ) ) ) ;
connect ( & m_salt_mine , SIGNAL ( collections_about_to_change ( ) ) , this , SLOT ( salt_mine_about_to_change ( ) ) ) ;
2017-03-18 22:36:33 +01:00
2017-03-27 23:55:26 +02:00
update_models ( ) ;
2017-03-18 22:36:33 +01:00
connect ( salt_view - > selectionModel ( ) , SIGNAL ( currentChanged ( const QModelIndex & , const QModelIndex & ) ) , this , SLOT ( current_changed ( ) ) ) ;
2017-03-27 23:55:26 +02:00
connect ( salt_view , SIGNAL ( doubleClicked ( const QModelIndex & ) ) , this , SLOT ( edit_properties ( ) ) ) ;
2017-04-22 18:09:12 +02:00
connect ( salt_mine_view_new - > selectionModel ( ) , SIGNAL ( currentChanged ( const QModelIndex & , const QModelIndex & ) ) , this , SLOT ( mine_new_current_changed ( ) ) , Qt : : QueuedConnection ) ;
connect ( salt_mine_view_update - > selectionModel ( ) , SIGNAL ( currentChanged ( const QModelIndex & , const QModelIndex & ) ) , this , SLOT ( mine_update_current_changed ( ) ) , Qt : : QueuedConnection ) ;
connect ( salt_mine_view_new , SIGNAL ( doubleClicked ( const QModelIndex & ) ) , this , SLOT ( mark_clicked ( ) ) ) ;
connect ( salt_mine_view_update , SIGNAL ( doubleClicked ( const QModelIndex & ) ) , this , SLOT ( mark_clicked ( ) ) ) ;
2017-03-18 22:36:33 +01:00
2017-03-25 23:33:07 +01:00
search_installed_edit - > set_clear_button_enabled ( true ) ;
search_new_edit - > set_clear_button_enabled ( true ) ;
2017-04-22 18:09:12 +02:00
search_update_edit - > set_clear_button_enabled ( true ) ;
2017-03-25 23:33:07 +01:00
connect ( search_installed_edit , SIGNAL ( textChanged ( const QString & ) ) , this , SLOT ( search_text_changed ( const QString & ) ) ) ;
connect ( search_new_edit , SIGNAL ( textChanged ( const QString & ) ) , this , SLOT ( search_text_changed ( const QString & ) ) ) ;
2017-04-22 18:09:12 +02:00
connect ( search_update_edit , SIGNAL ( textChanged ( const QString & ) ) , this , SLOT ( search_text_changed ( const QString & ) ) ) ;
connect ( mark_new_button , SIGNAL ( clicked ( ) ) , this , SLOT ( mark_clicked ( ) ) ) ;
connect ( mark_update_button , SIGNAL ( clicked ( ) ) , this , SLOT ( mark_clicked ( ) ) ) ;
QAction * a ;
2017-03-26 01:02:40 +01:00
2017-04-22 18:09:12 +02:00
salt_mine_view_new - > addAction ( actionUnmarkAllNew ) ;
a = new QAction ( this ) ;
a - > setSeparator ( true ) ;
salt_mine_view_new - > addAction ( a ) ;
salt_mine_view_new - > addAction ( actionShowMarkedOnlyNew ) ;
salt_mine_view_new - > addAction ( actionShowAllNew ) ;
a = new QAction ( this ) ;
a - > setSeparator ( true ) ;
salt_mine_view_new - > addAction ( a ) ;
salt_mine_view_new - > addAction ( actionRefresh ) ;
salt_mine_view_new - > setContextMenuPolicy ( Qt : : ActionsContextMenu ) ;
2017-03-27 23:55:26 +02:00
2017-04-22 18:09:12 +02:00
salt_mine_view_update - > addAction ( actionUnmarkAllUpdate ) ;
a = new QAction ( this ) ;
2017-03-27 23:55:26 +02:00
a - > setSeparator ( true ) ;
2017-04-22 18:09:12 +02:00
salt_mine_view_update - > addAction ( a ) ;
salt_mine_view_update - > addAction ( actionShowMarkedOnlyUpdate ) ;
salt_mine_view_update - > addAction ( actionShowAllUpdate ) ;
a = new QAction ( this ) ;
a - > setSeparator ( true ) ;
salt_mine_view_update - > addAction ( a ) ;
salt_mine_view_update - > addAction ( actionRefresh ) ;
salt_mine_view_update - > setContextMenuPolicy ( Qt : : ActionsContextMenu ) ;
connect ( actionUnmarkAllNew , SIGNAL ( triggered ( ) ) , this , SLOT ( unmark_all_new ( ) ) ) ;
connect ( actionShowMarkedOnlyNew , SIGNAL ( triggered ( ) ) , this , SLOT ( show_marked_only_new ( ) ) ) ;
connect ( actionShowAllNew , SIGNAL ( triggered ( ) ) , this , SLOT ( show_all_new ( ) ) ) ;
connect ( actionUnmarkAllUpdate , SIGNAL ( triggered ( ) ) , this , SLOT ( unmark_all_update ( ) ) ) ;
connect ( actionShowMarkedOnlyUpdate , SIGNAL ( triggered ( ) ) , this , SLOT ( show_marked_only_update ( ) ) ) ;
connect ( actionShowAllUpdate , SIGNAL ( triggered ( ) ) , this , SLOT ( show_all_update ( ) ) ) ;
connect ( actionRefresh , SIGNAL ( triggered ( ) ) , this , SLOT ( refresh ( ) ) ) ;
2017-03-19 22:44:46 +01:00
}
2017-03-18 22:36:33 +01:00
2017-03-25 10:15:24 +01:00
void
SaltManagerDialog : : mode_changed ( )
{
2017-04-22 21:30:39 +02:00
// commits edits:
setFocus ( Qt : : NoFocusReason ) ;
2017-04-22 18:09:12 +02:00
QList < int > sizes ;
if ( m_current_tab = = 0 ) {
sizes = splitter - > sizes ( ) ;
} else if ( m_current_tab = = 1 ) {
sizes = splitter_update - > sizes ( ) ;
} else if ( m_current_tab = = 2 ) {
sizes = splitter_new - > sizes ( ) ;
}
2017-03-25 10:15:24 +01:00
// keeps the splitters in sync
2017-04-22 18:09:12 +02:00
if ( ! sizes . empty ( ) ) {
splitter_new - > setSizes ( sizes ) ;
splitter_update - > setSizes ( sizes ) ;
splitter - > setSizes ( sizes ) ;
}
if ( mode_tab - > currentIndex ( ) > = 1 ) {
show_all_new ( ) ;
show_all_update ( ) ;
}
m_current_tab = mode_tab - > currentIndex ( ) ;
}
void
SaltManagerDialog : : show_all_new ( )
{
search_new_edit - > clear ( ) ;
SaltModel * model = dynamic_cast < SaltModel * > ( salt_mine_view_new - > model ( ) ) ;
if ( model ) {
for ( int i = model - > rowCount ( QModelIndex ( ) ) ; i > 0 ; ) {
- - i ;
salt_mine_view_new - > setRowHidden ( i , false ) ;
}
2017-03-25 10:15:24 +01:00
}
}
2017-03-27 23:55:26 +02:00
void
2017-04-22 18:09:12 +02:00
SaltManagerDialog : : show_all_update ( )
{
search_update_edit - > clear ( ) ;
SaltModel * model = dynamic_cast < SaltModel * > ( salt_mine_view_update - > model ( ) ) ;
if ( model ) {
for ( int i = model - > rowCount ( QModelIndex ( ) ) ; i > 0 ; ) {
- - i ;
salt_mine_view_update - > setRowHidden ( i , false ) ;
}
}
}
void
SaltManagerDialog : : show_marked_only_new ( )
2017-03-27 23:55:26 +02:00
{
search_new_edit - > clear ( ) ;
2017-04-22 18:09:12 +02:00
SaltModel * model = dynamic_cast < SaltModel * > ( salt_mine_view_new - > model ( ) ) ;
2017-03-27 23:55:26 +02:00
if ( ! model ) {
return ;
}
2017-04-22 18:09:12 +02:00
salt_mine_view_new - > setCurrentIndex ( QModelIndex ( ) ) ;
2017-03-27 23:55:26 +02:00
for ( int i = model - > rowCount ( QModelIndex ( ) ) ; i > 0 ; ) {
- - i ;
2017-04-22 18:09:12 +02:00
SaltGrain * g = model - > grain_from_index ( model - > index ( i , 0 , QModelIndex ( ) ) ) ;
salt_mine_view_new - > setRowHidden ( i , ! ( g & & model - > is_marked ( g - > name ( ) ) ) ) ;
mine_new_current_changed ( ) ;
2017-03-27 23:55:26 +02:00
}
}
void
2017-04-22 18:09:12 +02:00
SaltManagerDialog : : show_marked_only_update ( )
2017-03-27 23:55:26 +02:00
{
2017-04-22 18:09:12 +02:00
search_update_edit - > clear ( ) ;
2017-03-27 23:55:26 +02:00
2017-04-22 18:09:12 +02:00
SaltModel * model = dynamic_cast < SaltModel * > ( salt_mine_view_update - > model ( ) ) ;
2017-03-27 23:55:26 +02:00
if ( ! model ) {
return ;
}
2017-04-22 18:09:12 +02:00
salt_mine_view_update - > setCurrentIndex ( QModelIndex ( ) ) ;
2017-03-27 23:55:26 +02:00
for ( int i = model - > rowCount ( QModelIndex ( ) ) ; i > 0 ; ) {
- - i ;
SaltGrain * g = model - > grain_from_index ( model - > index ( i , 0 , QModelIndex ( ) ) ) ;
2017-04-22 18:09:12 +02:00
salt_mine_view_update - > setRowHidden ( i , ! ( g & & model - > is_marked ( g - > name ( ) ) ) ) ;
mine_update_current_changed ( ) ;
2017-03-27 23:55:26 +02:00
}
}
void
2017-04-22 18:09:12 +02:00
SaltManagerDialog : : unmark_all_new ( )
2017-03-27 23:55:26 +02:00
{
2017-04-22 18:09:12 +02:00
SaltModel * model = dynamic_cast < SaltModel * > ( salt_mine_view_new - > model ( ) ) ;
2017-03-27 23:55:26 +02:00
if ( model ) {
model - > clear_marked ( ) ;
2017-04-22 18:09:12 +02:00
show_all_new ( ) ;
update_apply_state ( ) ;
}
}
void
SaltManagerDialog : : unmark_all_update ( )
{
SaltModel * model = dynamic_cast < SaltModel * > ( salt_mine_view_update - > model ( ) ) ;
if ( model ) {
model - > clear_marked ( ) ;
show_all_update ( ) ;
2017-03-27 23:55:26 +02:00
update_apply_state ( ) ;
}
}
2017-03-25 23:33:07 +01:00
void
SaltManagerDialog : : search_text_changed ( const QString & text )
{
QListView * view = 0 ;
if ( sender ( ) = = search_installed_edit ) {
view = salt_view ;
} else if ( sender ( ) = = search_new_edit ) {
2017-04-22 18:09:12 +02:00
view = salt_mine_view_new ;
} else if ( sender ( ) = = search_update_edit ) {
view = salt_mine_view_update ;
2017-03-25 23:33:07 +01:00
} else {
return ;
}
SaltModel * model = dynamic_cast < SaltModel * > ( view - > model ( ) ) ;
if ( ! model ) {
return ;
}
if ( text . isEmpty ( ) ) {
for ( int i = model - > rowCount ( QModelIndex ( ) ) ; i > 0 ; ) {
- - i ;
view - > setRowHidden ( i , false ) ;
}
} else {
QRegExp re ( text , Qt : : CaseInsensitive ) ;
for ( int i = model - > rowCount ( QModelIndex ( ) ) ; i > 0 ; ) {
- - i ;
QModelIndex index = model - > index ( i , 0 , QModelIndex ( ) ) ;
SaltGrain * g = model - > grain_from_index ( index ) ;
bool hidden = ( ! g | | re . indexIn ( tl : : to_qstring ( g - > name ( ) ) ) < 0 ) ;
view - > setRowHidden ( i , hidden ) ;
}
}
}
2017-03-26 01:02:40 +01:00
void
SaltManagerDialog : : mark_clicked ( )
{
2017-04-22 18:09:12 +02:00
QListView * view ;
if ( sender ( ) = = salt_mine_view_new | | sender ( ) = = mark_new_button ) {
view = salt_mine_view_new ;
} else {
view = salt_mine_view_update ;
2017-03-26 01:02:40 +01:00
}
2017-04-22 18:09:12 +02:00
SaltModel * model = dynamic_cast < SaltModel * > ( view - > model ( ) ) ;
if ( ! model ) {
2017-03-26 01:02:40 +01:00
return ;
}
2017-04-22 18:09:12 +02:00
SaltGrain * g = model - > grain_from_index ( view - > currentIndex ( ) ) ;
if ( g ) {
model - > set_marked ( g - > name ( ) , ! model - > is_marked ( g - > name ( ) ) ) ;
update_apply_state ( ) ;
}
2017-03-27 23:55:26 +02:00
}
void
SaltManagerDialog : : update_apply_state ( )
{
2017-04-22 18:09:12 +02:00
SaltModel * model ;
2017-03-27 23:55:26 +02:00
2017-04-22 18:09:12 +02:00
model = dynamic_cast < SaltModel * > ( salt_mine_view_new - > model ( ) ) ;
if ( model ) {
2017-03-27 23:55:26 +02:00
2017-04-22 18:09:12 +02:00
int marked = 0 ;
for ( int i = model - > rowCount ( QModelIndex ( ) ) ; i > 0 ; ) {
- - i ;
QModelIndex index = model - > index ( i , 0 , QModelIndex ( ) ) ;
SaltGrain * g = model - > grain_from_index ( index ) ;
if ( g & & model - > is_marked ( g - > name ( ) ) ) {
marked + = 1 ;
}
}
apply_new_button - > setEnabled ( marked > 0 ) ;
if ( marked = = 0 ) {
apply_label_new - > setText ( QString ( ) ) ;
} else if ( marked = = 1 ) {
apply_label_new - > setText ( tr ( " One package selected " ) ) ;
} else if ( marked > 1 ) {
apply_label_new - > setText ( tr ( " %1 packages selected " ) . arg ( marked ) ) ;
2017-03-27 23:55:26 +02:00
}
2017-04-22 18:09:12 +02:00
2017-03-27 23:55:26 +02:00
}
2017-04-22 18:09:12 +02:00
model = dynamic_cast < SaltModel * > ( salt_mine_view_update - > model ( ) ) ;
if ( model ) {
int marked = 0 ;
for ( int i = model - > rowCount ( QModelIndex ( ) ) ; i > 0 ; ) {
- - i ;
QModelIndex index = model - > index ( i , 0 , QModelIndex ( ) ) ;
SaltGrain * g = model - > grain_from_index ( index ) ;
if ( g & & model - > is_marked ( g - > name ( ) ) ) {
marked + = 1 ;
}
}
apply_update_button - > setEnabled ( marked > 0 ) ;
if ( marked = = 0 ) {
apply_label_update - > setText ( QString ( ) ) ;
} else if ( marked = = 1 ) {
apply_label_update - > setText ( tr ( " One package selected " ) ) ;
} else if ( marked > 1 ) {
apply_label_update - > setText ( tr ( " %1 packages selected " ) . arg ( marked ) ) ;
}
2017-03-27 23:55:26 +02:00
}
2017-03-26 01:02:40 +01:00
}
2017-03-27 15:46:01 +02:00
void
SaltManagerDialog : : apply ( )
{
BEGIN_PROTECTED
2017-04-22 18:09:12 +02:00
bool update = ( sender ( ) = = apply_update_button ) ;
2017-03-27 15:46:01 +02:00
lay : : SaltDownloadManager manager ;
bool any = false ;
// fetch all marked grains and register for download
2017-04-22 18:09:12 +02:00
SaltModel * model ;
if ( update ) {
model = dynamic_cast < SaltModel * > ( salt_mine_view_update - > model ( ) ) ;
} else {
model = dynamic_cast < SaltModel * > ( salt_mine_view_new - > model ( ) ) ;
}
2017-03-27 15:46:01 +02:00
if ( model ) {
for ( int i = model - > rowCount ( QModelIndex ( ) ) ; i > 0 ; ) {
- - i ;
QModelIndex index = model - > index ( i , 0 , QModelIndex ( ) ) ;
SaltGrain * g = model - > grain_from_index ( index ) ;
if ( g & & model - > is_marked ( g - > name ( ) ) ) {
manager . register_download ( g - > name ( ) , g - > url ( ) , g - > version ( ) ) ;
any = true ;
}
}
}
if ( ! any ) {
2017-04-22 18:09:12 +02:00
if ( update ) {
throw tl : : Exception ( tl : : to_string ( tr ( " No packages marked for update " ) ) ) ;
} else {
throw tl : : Exception ( tl : : to_string ( tr ( " No packages marked for installation " ) ) ) ;
}
2017-03-27 15:46:01 +02:00
}
2017-04-22 18:09:12 +02:00
manager . compute_dependencies ( * mp_salt , m_salt_mine ) ;
2017-03-27 15:46:01 +02:00
if ( manager . show_confirmation_dialog ( this , * mp_salt ) ) {
2017-04-22 18:09:12 +02:00
if ( update ) {
unmark_all_update ( ) ;
} else {
unmark_all_new ( ) ;
}
2017-03-27 15:46:01 +02:00
manager . execute ( * mp_salt ) ;
}
END_PROTECTED
}
2017-03-19 22:44:46 +01:00
void
SaltManagerDialog : : edit_properties ( )
{
SaltGrain * g = current_grain ( ) ;
if ( g ) {
2017-04-22 21:56:37 +02:00
if ( g - > is_readonly ( ) ) {
QMessageBox : : critical ( this , tr ( " Package is not Editable " ) ,
tr ( " This package cannot be edited. \n \n Either you don't have write permissions on the directory or the package was installed from a repository. " ) ) ;
} else if ( mp_properties_dialog - > exec_dialog ( g , mp_salt ) ) {
2017-03-19 22:44:46 +01:00
current_changed ( ) ;
}
}
2017-03-12 23:26:04 +01:00
}
2017-03-23 22:19:13 +01:00
void
SaltManagerDialog : : create_grain ( )
{
2017-03-23 23:46:25 +01:00
BEGIN_PROTECTED
SaltGrainTemplateSelectionDialog temp_dialog ( this , mp_salt ) ;
2017-03-23 22:19:13 +01:00
if ( temp_dialog . exec ( ) ) {
2017-03-23 23:46:25 +01:00
SaltGrain target ;
target . set_name ( temp_dialog . name ( ) ) ;
if ( mp_salt - > create_grain ( temp_dialog . templ ( ) , target ) ) {
// select the new one
SaltModel * model = dynamic_cast < SaltModel * > ( salt_view - > model ( ) ) ;
if ( model ) {
for ( int i = model - > rowCount ( QModelIndex ( ) ) ; i > 0 ; ) {
- - i ;
QModelIndex index = model - > index ( i , 0 , QModelIndex ( ) ) ;
SaltGrain * g = model - > grain_from_index ( index ) ;
if ( g & & g - > name ( ) = = target . name ( ) ) {
salt_view - > setCurrentIndex ( index ) ;
break ;
}
}
}
} else {
throw tl : : Exception ( tl : : to_string ( tr ( " Initialization of new package failed - see log window (File/Log Viewer) for details " ) ) ) ;
}
2017-03-23 22:19:13 +01:00
}
2017-03-23 23:46:25 +01:00
END_PROTECTED
2017-03-23 22:19:13 +01:00
}
void
SaltManagerDialog : : delete_grain ( )
{
2017-03-25 00:08:17 +01:00
BEGIN_PROTECTED
2017-03-23 22:19:13 +01:00
2017-03-25 00:08:17 +01:00
SaltGrain * g = current_grain ( ) ;
if ( ! g ) {
throw tl : : Exception ( tl : : to_string ( tr ( " No package selected to delete " ) ) ) ;
}
2017-03-23 22:19:13 +01:00
2017-03-25 00:08:17 +01:00
if ( QMessageBox : : question ( this , tr ( " Delete Package " ) , tr ( " Are you sure to delete package '%1'? " ) . arg ( tl : : to_qstring ( g - > name ( ) ) ) , QMessageBox : : Yes , QMessageBox : : No ) = = QMessageBox : : Yes ) {
mp_salt - > remove_grain ( * g ) ;
}
END_PROTECTED
2017-03-23 22:19:13 +01:00
}
2017-04-20 00:09:53 +02:00
void
SaltManagerDialog : : salt_about_to_change ( )
{
SaltModel * model = dynamic_cast < SaltModel * > ( salt_view - > model ( ) ) ;
tl_assert ( model ! = 0 ) ;
model - > begin_update ( ) ;
}
2017-03-18 22:36:33 +01:00
void
SaltManagerDialog : : salt_changed ( )
2017-03-27 23:55:26 +02:00
{
dm_update_models ( ) ;
}
2017-04-20 00:09:53 +02:00
void
SaltManagerDialog : : salt_mine_about_to_change ( )
{
2017-04-22 18:09:12 +02:00
SaltModel * model ;
model = dynamic_cast < SaltModel * > ( salt_mine_view_new - > model ( ) ) ;
tl_assert ( model ! = 0 ) ;
model - > begin_update ( ) ;
model = dynamic_cast < SaltModel * > ( salt_mine_view_update - > model ( ) ) ;
tl_assert ( model ! = 0 ) ;
model - > begin_update ( ) ;
}
void
SaltManagerDialog : : refresh ( )
{
BEGIN_PROTECTED
if ( ! m_salt_mine_url . empty ( ) ) {
tl : : log < < tl : : to_string ( tr ( " Downloading package repository from %1 " ) . arg ( tl : : to_qstring ( m_salt_mine_url ) ) ) ;
lay : : Salt new_mine ;
new_mine . load ( m_salt_mine_url ) ;
m_salt_mine = new_mine ;
salt_mine_changed ( ) ;
}
END_PROTECTED
2017-04-20 00:09:53 +02:00
}
2017-03-27 23:55:26 +02:00
void
SaltManagerDialog : : salt_mine_changed ( )
{
dm_update_models ( ) ;
}
void
SaltManagerDialog : : update_models ( )
2017-03-18 22:36:33 +01:00
{
SaltModel * model = dynamic_cast < SaltModel * > ( salt_view - > model ( ) ) ;
2017-03-27 23:55:26 +02:00
tl_assert ( model ! = 0 ) ;
2017-03-18 22:36:33 +01:00
2017-03-27 23:55:26 +02:00
model - > clear_messages ( ) ;
// Establish a message saying that an update is available
for ( Salt : : flat_iterator g = mp_salt - > begin_flat ( ) ; g ! = mp_salt - > end_flat ( ) ; + + g ) {
2017-04-22 18:09:12 +02:00
SaltGrain * gm = m_salt_mine . grain_by_name ( ( * g ) - > name ( ) ) ;
2017-03-27 23:55:26 +02:00
if ( gm & & SaltGrain : : compare_versions ( gm - > version ( ) , ( * g ) - > version ( ) ) > 0 ) {
model - > set_message ( ( * g ) - > name ( ) , SaltModel : : Warning , tl : : to_string ( tr ( " An update to version %1 is available " ) . arg ( tl : : to_qstring ( gm - > version ( ) ) ) ) ) ;
}
}
2017-03-18 22:36:33 +01:00
model - > update ( ) ;
2017-03-27 23:55:26 +02:00
2017-03-18 22:36:33 +01:00
if ( mp_salt - > is_empty ( ) ) {
2017-03-23 23:46:25 +01:00
2017-03-18 22:36:33 +01:00
list_stack - > setCurrentIndex ( 1 ) ;
details_frame - > hide ( ) ;
2017-03-23 23:46:25 +01:00
2017-03-18 22:36:33 +01:00
} else {
2017-03-23 23:46:25 +01:00
2017-03-18 22:36:33 +01:00
list_stack - > setCurrentIndex ( 0 ) ;
details_frame - > show ( ) ;
2017-03-23 23:46:25 +01:00
// select the first grain
if ( model - > rowCount ( QModelIndex ( ) ) > 0 ) {
salt_view - > setCurrentIndex ( model - > index ( 0 , 0 , QModelIndex ( ) ) ) ;
}
2017-03-18 22:36:33 +01:00
}
2017-04-22 18:09:12 +02:00
SaltModel * mine_model ;
2017-03-27 23:55:26 +02:00
2017-04-22 18:09:12 +02:00
mine_model = dynamic_cast < SaltModel * > ( salt_mine_view_update - > model ( ) ) ;
tl_assert ( mine_model ! = 0 ) ;
2017-03-27 23:55:26 +02:00
mine_model - > clear_order ( ) ;
mine_model - > clear_messages ( ) ;
mine_model - > enable_all ( ) ;
2017-04-22 18:09:12 +02:00
bool has_warning = false ;
2017-03-27 23:55:26 +02:00
// Establish a message saying that an update is available
for ( Salt : : flat_iterator g = mp_salt - > begin_flat ( ) ; g ! = mp_salt - > end_flat ( ) ; + + g ) {
2017-04-22 18:09:12 +02:00
SaltGrain * gm = m_salt_mine . grain_by_name ( ( * g ) - > name ( ) ) ;
2017-03-27 23:55:26 +02:00
if ( gm & & SaltGrain : : compare_versions ( gm - > version ( ) , ( * g ) - > version ( ) ) > 0 ) {
2017-04-22 18:09:12 +02:00
has_warning = true ;
2017-03-27 23:55:26 +02:00
mine_model - > set_message ( ( * g ) - > name ( ) , SaltModel : : Warning , tl : : to_string ( tr ( " The installed version is outdated (%1) " ) . arg ( tl : : to_qstring ( ( * g ) - > version ( ) ) ) ) ) ;
mine_model - > set_order ( ( * g ) - > name ( ) , - 1 ) ;
} else if ( gm ) {
2017-04-22 18:09:12 +02:00
mine_model - > set_message ( ( * g ) - > name ( ) , SaltModel : : None , tl : : to_string ( tr ( " This package is up to date " ) ) ) ;
2017-03-27 23:55:26 +02:00
mine_model - > set_order ( ( * g ) - > name ( ) , 1 ) ;
mine_model - > set_enabled ( ( * g ) - > name ( ) , false ) ;
}
}
2017-04-22 21:45:49 +02:00
// Establish a message indicating whether the API version does not match
for ( Salt : : flat_iterator g = m_salt_mine . begin_flat ( ) ; g ! = m_salt_mine . end_flat ( ) ; + + g ) {
if ( SaltGrain : : compare_versions ( lay : : Version : : version ( ) , ( * g ) - > api_version ( ) ) < 0 ) {
mine_model - > set_message ( ( * g ) - > name ( ) , SaltModel : : Warning , tl : : to_string ( tr ( " This package requires a newer API (%1) " ) . arg ( tl : : to_qstring ( ( * g ) - > api_version ( ) ) ) ) ) ;
mine_model - > set_enabled ( ( * g ) - > name ( ) , false ) ;
}
}
2017-04-22 18:09:12 +02:00
if ( has_warning ) {
mode_tab - > setTabIcon ( 1 , QIcon ( " :/warn_16.png " ) ) ;
} else {
mode_tab - > setTabIcon ( 1 , QIcon ( ) ) ;
}
2017-03-27 23:55:26 +02:00
mine_model - > update ( ) ;
2017-04-22 18:09:12 +02:00
// select the first grain
if ( mine_model - > rowCount ( QModelIndex ( ) ) > 0 ) {
salt_mine_view_update - > setCurrentIndex ( mine_model - > index ( 0 , 0 , QModelIndex ( ) ) ) ;
}
mine_model = dynamic_cast < SaltModel * > ( salt_mine_view_new - > model ( ) ) ;
tl_assert ( mine_model ! = 0 ) ;
mine_model - > clear_order ( ) ;
mine_model - > clear_messages ( ) ;
mine_model - > enable_all ( ) ;
2017-04-22 21:45:49 +02:00
// Establish a message indicating whether the API version does not match
for ( Salt : : flat_iterator g = m_salt_mine . begin_flat ( ) ; g ! = m_salt_mine . end_flat ( ) ; + + g ) {
if ( SaltGrain : : compare_versions ( lay : : Version : : version ( ) , ( * g ) - > api_version ( ) ) < 0 ) {
mine_model - > set_message ( ( * g ) - > name ( ) , SaltModel : : Warning , tl : : to_string ( tr ( " This package requires a newer API (%1) " ) . arg ( tl : : to_qstring ( ( * g ) - > api_version ( ) ) ) ) ) ;
mine_model - > set_enabled ( ( * g ) - > name ( ) , false ) ;
}
}
2017-04-22 18:09:12 +02:00
mine_model - > update ( ) ;
2017-03-27 23:55:26 +02:00
// select the first grain
if ( mine_model - > rowCount ( QModelIndex ( ) ) > 0 ) {
2017-04-22 18:09:12 +02:00
salt_mine_view_new - > setCurrentIndex ( mine_model - > index ( 0 , 0 , QModelIndex ( ) ) ) ;
2017-03-27 23:55:26 +02:00
}
2017-04-22 18:09:12 +02:00
mine_new_current_changed ( ) ;
mine_update_current_changed ( ) ;
2017-03-18 22:36:33 +01:00
current_changed ( ) ;
2017-03-27 23:55:26 +02:00
update_apply_state ( ) ;
2017-03-18 22:36:33 +01:00
}
void
SaltManagerDialog : : current_changed ( )
{
2017-03-19 22:44:46 +01:00
SaltGrain * g = current_grain ( ) ;
2017-03-18 22:36:33 +01:00
details_text - > set_grain ( g ) ;
if ( ! g ) {
details_frame - > setEnabled ( false ) ;
delete_button - > setEnabled ( false ) ;
} else {
details_frame - > setEnabled ( true ) ;
delete_button - > setEnabled ( true ) ;
2017-03-21 22:20:24 +01:00
edit_button - > setEnabled ( ! g - > is_readonly ( ) ) ;
2017-03-18 22:36:33 +01:00
}
}
2017-03-19 22:44:46 +01:00
lay : : SaltGrain *
SaltManagerDialog : : current_grain ( )
{
SaltModel * model = dynamic_cast < SaltModel * > ( salt_view - > model ( ) ) ;
return model ? model - > grain_from_index ( salt_view - > currentIndex ( ) ) : 0 ;
}
2017-03-25 10:15:24 +01:00
void
2017-04-22 18:09:12 +02:00
SaltManagerDialog : : mine_update_current_changed ( )
2017-03-25 10:15:24 +01:00
{
2017-04-22 18:09:12 +02:00
BEGIN_PROTECTED
SaltModel * model = dynamic_cast < SaltModel * > ( salt_mine_view_update - > model ( ) ) ;
tl_assert ( model ! = 0 ) ;
SaltGrain * g = model - > grain_from_index ( salt_mine_view_update - > currentIndex ( ) ) ;
details_update_frame - > setEnabled ( g ! = 0 ) ;
2017-04-22 21:30:39 +02:00
SaltGrain * remote_grain = get_remote_grain_info ( g , details_update_text ) ;
2017-04-22 18:09:12 +02:00
m_remote_update_grain . reset ( remote_grain ) ;
END_PROTECTED
}
2017-03-26 01:02:40 +01:00
2017-04-22 18:09:12 +02:00
void
SaltManagerDialog : : mine_new_current_changed ( )
{
2017-03-25 23:33:07 +01:00
BEGIN_PROTECTED
2017-04-22 18:09:12 +02:00
SaltModel * model = dynamic_cast < SaltModel * > ( salt_mine_view_new - > model ( ) ) ;
tl_assert ( model ! = 0 ) ;
SaltGrain * g = model - > grain_from_index ( salt_mine_view_new - > currentIndex ( ) ) ;
2017-03-25 10:15:24 +01:00
details_new_frame - > setEnabled ( g ! = 0 ) ;
2017-03-25 23:33:07 +01:00
2017-04-22 21:30:39 +02:00
SaltGrain * remote_grain = get_remote_grain_info ( g , details_new_text ) ;
2017-04-22 18:09:12 +02:00
m_remote_new_grain . reset ( remote_grain ) ;
END_PROTECTED
}
lay : : SaltGrain *
2017-04-22 21:30:39 +02:00
SaltManagerDialog : : get_remote_grain_info ( lay : : SaltGrain * g , SaltGrainDetailsTextWidget * details )
2017-04-22 18:09:12 +02:00
{
2017-03-25 23:33:07 +01:00
if ( ! g ) {
2017-04-22 18:09:12 +02:00
return 0 ;
2017-03-25 23:33:07 +01:00
}
2017-04-22 18:09:12 +02:00
std : : auto_ptr < lay : : SaltGrain > remote_grain ;
remote_grain . reset ( 0 ) ;
2017-03-25 23:33:07 +01:00
// Download actual grain definition file
try {
if ( g - > url ( ) . empty ( ) ) {
throw tl : : Exception ( tl : : to_string ( tr ( " No download link available " ) ) ) ;
}
2017-04-22 21:30:39 +02:00
QString html = tr (
2017-03-26 01:02:40 +01:00
" <html> "
" <body> "
" <font color= \" #c0c0c0 \" > "
" <h2>Fetching Package Definition ...</h2> "
" <p><b>URL</b>: %1</p> "
" </font> "
" </body> "
" </html> "
)
. arg ( tl : : to_qstring ( SaltGrain : : spec_url ( g - > url ( ) ) ) ) ;
2017-04-22 21:30:39 +02:00
details - > setHtml ( html ) ;
2017-03-26 01:02:40 +01:00
QApplication : : processEvents ( QEventLoop : : ExcludeUserInputEvents ) ;
2017-04-17 18:24:59 +02:00
tl : : InputStream stream ( SaltGrain : : spec_url ( g - > url ( ) ) ) ;
2017-03-25 23:33:07 +01:00
2017-04-22 18:09:12 +02:00
remote_grain . reset ( new SaltGrain ( ) ) ;
remote_grain - > load ( stream ) ;
remote_grain - > set_url ( g - > url ( ) ) ;
2017-03-25 23:33:07 +01:00
2017-04-22 18:09:12 +02:00
if ( g - > name ( ) ! = remote_grain - > name ( ) ) {
throw tl : : Exception ( tl : : to_string ( tr ( " Name mismatch between repository and actual package (repository: %1, package: %2) " ) . arg ( tl : : to_qstring ( g - > name ( ) ) ) . arg ( tl : : to_qstring ( remote_grain - > name ( ) ) ) ) ) ;
2017-03-25 23:33:07 +01:00
}
2017-04-22 18:09:12 +02:00
if ( SaltGrain : : compare_versions ( g - > version ( ) , remote_grain - > version ( ) ) ! = 0 ) {
throw tl : : Exception ( tl : : to_string ( tr ( " Version mismatch between repository and actual package (repository: %1, package: %2) " ) . arg ( tl : : to_qstring ( g - > version ( ) ) ) . arg ( tl : : to_qstring ( remote_grain - > version ( ) ) ) ) ) ;
2017-03-25 23:33:07 +01:00
}
2017-04-22 21:30:39 +02:00
details - > set_grain ( remote_grain . get ( ) ) ;
2017-03-25 23:33:07 +01:00
} catch ( tl : : Exception & ex ) {
2017-04-22 18:09:12 +02:00
remote_grain . reset ( 0 ) ;
2017-03-25 23:33:07 +01:00
2017-04-22 21:30:39 +02:00
QString html = tr (
2017-03-25 23:33:07 +01:00
" <html> "
" <body> "
" <font color= \" #ff0000 \" > "
2017-03-26 01:02:40 +01:00
" <h2>Error Fetching Package Definition</h2> "
" <p><b>URL</b>: %1</p> "
" <p><b>Error</b>: %2</p> "
" </font> "
2017-03-25 23:33:07 +01:00
" </body> "
" </html> "
)
. arg ( tl : : to_qstring ( SaltGrain : : spec_url ( g - > url ( ) ) ) )
. arg ( tl : : to_qstring ( tl : : escaped_to_html ( ex . msg ( ) ) ) ) ;
2017-04-22 21:30:39 +02:00
details - > setHtml ( html ) ;
2017-03-25 23:33:07 +01:00
}
2017-04-22 18:09:12 +02:00
return remote_grain . release ( ) ;
2017-03-25 10:15:24 +01:00
}
2017-03-12 23:26:04 +01:00
}