1 /* wconfig_pathjump_allow.cc
2  * This file belongs to Worker, a file manager for UN*X/X11.
3  * Copyright (C) 2012-2020 Ralf Hoffmann.
4  * You can contact me at: ralf@boomerangsworld.de
5  *   or http://www.boomerangsworld.de/worker
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include "wconfig_pathjump_allow.hh"
23 #include "wconfig.h"
24 #include "worker.h"
25 #include "worker_locale.h"
26 #include "aguix/fieldlistview.h"
27 #include "aguix/button.h"
28 #include "aguix/choosebutton.h"
29 #include "datei.h"
30 #include "nwc_path.hh"
31 
PathJumpAllowPanel(AWindow & basewin,WConfig & baseconfig)32 PathJumpAllowPanel::PathJumpAllowPanel( AWindow &basewin, WConfig &baseconfig ) : WConfigPanel( basewin, baseconfig )
33 {
34     m_lv = NULL;
35 }
36 
~PathJumpAllowPanel()37 PathJumpAllowPanel::~PathJumpAllowPanel()
38 {
39 }
40 
create()41 int PathJumpAllowPanel::create()
42 {
43     Panel::create();
44 
45     int trow;
46 
47     AContainer *ac1 = setContainer( new AContainer( this, 1, 4 ), true );
48     ac1->setMinSpace( 5 );
49     ac1->setMaxSpace( 5 );
50 
51     addMultiLineText( catalog.getLocale( 975 ),
52                       *ac1,
53                       0, 0,
54                       NULL, NULL );
55 
56     m_lv =(FieldListView*)ac1->add( new FieldListView( _aguix, 0, 0, 100, 100, 0 ), 0, 1, AContainer::CO_MIN );
57     m_lv->setHBarState(2);
58     m_lv->setVBarState(2);
59     m_lv->setAcceptFocus( true );
60     m_lv->setDisplayFocus( true );
61 
62     AContainer *ac1_1 = ac1->add( new AContainer( this, 2, 1 ), 0, 2 );
63     ac1_1->setMinSpace( 0 );
64     ac1_1->setMaxSpace( 0 );
65     ac1_1->setBorderWidth( 0 );
66 
67     m_newb = (Button*)ac1_1->add( new Button( _aguix, 0, 0, catalog.getLocale( 181 ), 0 ),
68                                   0, 0, AContainer::CO_INCW );
69     m_newb->connect( this );
70 
71     m_delb = (Button*)ac1_1->add( new Button( _aguix, 0, 0, catalog.getLocale( 183 ), 0 ),
72                                   1, 0, AContainer::CO_INCW );
73     m_delb->connect( this );
74 
75     m_store_files_cb = ac1->addWidget( new ChooseButton( _aguix, 0, 0,
76                                                          _baseconfig.getPathJumpStoreFilesAlways(),
77                                                          catalog.getLocale( 1043 ),
78                                                          LABEL_RIGHT,
79                                                          0 ), 0, 3, AContainer::CO_INCWNR );
80 
81     contMaximize( true );
82 
83     const std::list< std::string > &l = _baseconfig.getPathJumpAllowDirs();
84 
85     for ( std::list< std::string >::const_iterator it1 = l.begin();
86           it1 != l.end();
87           it1++ ) {
88         trow = m_lv->addRow();
89         m_lv->setPreColors( trow, FieldListView::PRECOLOR_ONLYACTIVE );
90         m_lv->setText( trow, 0, it1->c_str() );
91     }
92     m_lv->redraw();
93 
94     return 0;
95 }
96 
saveValues()97 int PathJumpAllowPanel::saveValues()
98 {
99     if ( m_lv == NULL ) return 1;
100 
101     std::list< std::string > l;
102 
103     int trow = 0;
104 
105     while ( m_lv->isValidRow( trow ) == true ) {
106         l.push_back( m_lv->getText( trow, 0 ) );
107         trow++;
108     }
109 
110     _baseconfig.setPathJumpAllowDirs( l );
111 
112     _baseconfig.setPathJumpStoreFilesAlways( m_store_files_cb->getState() );
113 
114     return 0;
115 }
116 
run(Widget * elem,const AGMessage & msg)117 void PathJumpAllowPanel::run( Widget *elem, const AGMessage &msg )
118 {
119     char *tstr, *tstr2;
120     bool found;
121     int trow;
122 
123     if ( msg.type == AG_BUTTONCLICKED ) {
124         if ( msg.button.button == m_newb ) {
125             std::string buttonstr = catalog.getLocale( 11 );
126             buttonstr += "|";
127             buttonstr += catalog.getLocale( 8 );
128 
129             if ( string_request( catalog.getLocale( 123 ), catalog.getLocale( 976 ), "", buttonstr.c_str(), &tstr ) == 0 ) {
130                 if ( strlen( tstr ) > 0 ) {
131                     if ( tstr[0] == '/' ) {
132                         tstr2 = NWC::Path::handlePath( tstr );
133 
134                         found = false;
135                         trow = 0;
136                         while ( m_lv->isValidRow( trow ) == true ) {
137                             if ( strcmp( m_lv->getText( trow, 0 ).c_str(), tstr2 ) == 0 ) {
138                                 found = true;
139                                 break;
140                             }
141                             trow++;
142                         }
143 
144                         if ( found == false ) {
145                             trow = m_lv->addRow();
146                             m_lv->setPreColors( trow, FieldListView::PRECOLOR_ONLYACTIVE );
147                             m_lv->setText( trow, 0, tstr2 );
148                             m_lv->setActiveRow( trow );
149                             m_lv->showActive();
150                             m_lv->redraw();
151                         }
152                         _freesafe( tstr2 );
153                     }
154                 }
155                 _freesafe( tstr );
156             } else _freesafe( tstr );
157         } else if ( msg.button.button == m_delb ) {
158             trow = m_lv->getActiveRow();
159             if ( m_lv->isValidRow( trow ) == true ) {
160                 m_lv->deleteRow( trow );
161                 m_lv->redraw();
162             }
163         }
164     }
165 }
166