File: pEnvironmentVariablesModel.cpp

package info (click to toggle)
monkeystudio 1.9.0.4%2Bgit20161218-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,500 kB
  • ctags: 22,118
  • sloc: cpp: 144,671; ansic: 33,969; python: 2,922; makefile: 127; sh: 122; php: 73; cs: 69
file content (364 lines) | stat: -rw-r--r-- 9,495 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/****************************************************************************
    Copyright (C) 2005 - 2011  Filipe AZEVEDO & The Monkey Studio Team
    http://monkeystudio.org licensing under the GNU GPL.

    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 "pEnvironmentVariablesModel.h"

#include <QStringList>
#include <QFont>
#include <QProcess>
#include <QDebug>

pEnvironmentVariablesModel::pEnvironmentVariablesModel( QObject* parent )
    : QAbstractItemModel( parent )
{
    mRowCount = 0;
}

int pEnvironmentVariablesModel::columnCount( const QModelIndex& parent ) const
{
    return parent.isValid() ? 0 : 2;
}

QVariant pEnvironmentVariablesModel::data( const QModelIndex& index, int role ) const
{
    if ( !index.isValid() )
    {
        return QVariant();
    }

    pEnvironmentVariablesModel::Variable* variable = static_cast<pEnvironmentVariablesModel::Variable*>( index.internalPointer() );

    switch ( role )
    {
        case Qt::DisplayRole:
            return index.column() == 0 ? variable->name : variable->value;
        case Qt::FontRole:
        {
            QFont font;
            font.setStrikeOut( !variable->enabled );
            return font;
        }
        case Qt::CheckStateRole:
        {
            if ( index.column() != 0 )
            {
                break;
            }

            return variable->enabled ? Qt::Checked : Qt::Unchecked;
        }
    }

    return QVariant();
}

QModelIndex pEnvironmentVariablesModel::index( int row, int column, const QModelIndex& parent ) const
{
    if ( parent.isValid() || column < 0 || column >= 2 || row < 0 || row >= mRowCount )
    {
        return QModelIndex();
    }

    pEnvironmentVariablesModel::Variable* variable = mOrder[ row ];
    return createIndex( row, column, variable );
}

QModelIndex pEnvironmentVariablesModel::parent( const QModelIndex& index ) const
{
    Q_UNUSED( index );
    return QModelIndex();
}

int pEnvironmentVariablesModel::rowCount( const QModelIndex& parent ) const
{
    return parent.isValid() ? 0 : mRowCount;
}

QVariant pEnvironmentVariablesModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
    if ( orientation == Qt::Vertical || section < 0 || section >= 2 )
    {
        return QAbstractItemModel::headerData( section, orientation, role );
    }

    switch ( role )
    {
        case Qt::DisplayRole:
            return section == 0 ? tr( "Name" ) : tr( "Value" );
    }

    return QAbstractItemModel::headerData( section, orientation, role );
}

bool pEnvironmentVariablesModel::hasChildren( const QModelIndex& parent ) const
{
    return parent.isValid() ? false : !mVariables.isEmpty();
}

Qt::ItemFlags pEnvironmentVariablesModel::flags( const QModelIndex& index ) const
{
    Qt::ItemFlags flags = QAbstractItemModel::flags( index );

    if ( !index.isValid() )
    {
        return flags;
    }

    if ( index.column() == 0 )
    {
        flags |= Qt::ItemIsUserCheckable;
    }

    return flags;
}

bool pEnvironmentVariablesModel::setData( const QModelIndex& index, const QVariant& value, int role )
{
    if ( !index.isValid() || index.column() != 0 )
    {
        return false;
    }

    pEnvironmentVariablesModel::Variable* variable = static_cast<pEnvironmentVariablesModel::Variable*>( index.internalPointer() );

    switch ( role )
    {
        case Qt::CheckStateRole:
        {
            variable->enabled = value.toInt() == Qt::Checked;
            emit dataChanged( index, index.sibling( index.row(), 1 ) );
        }
    }

    return false;
}

QModelIndex pEnvironmentVariablesModel::index( const QString& name, int column ) const
{
    if ( !mVariables.contains( name ) || column < 0 || column >= 2 )
    {
        return QModelIndex();
    }

    pEnvironmentVariablesModel::Variable& variable = mVariables[ name ];
    return createIndex( mOrder.indexOf( &variable ), column, &variable );
}

pEnvironmentVariablesModel::Variable pEnvironmentVariablesModel::variable( const QModelIndex& index ) const
{
    pEnvironmentVariablesModel::Variable variable;

    if ( index.isValid() )
    {
        variable = *static_cast<pEnvironmentVariablesModel::Variable*>( index.internalPointer() );
    }

    return variable;
}

const pEnvironmentVariablesModel::Variables& pEnvironmentVariablesModel::variables() const
{
    return mVariables;
}

const pEnvironmentVariablesModel::Variables& pEnvironmentVariablesModel::defaultVariables() const
{
    return mDefaultVariables;
}

QStringList pEnvironmentVariablesModel::variables( bool keepDisabled ) const
{
    return variablesToStringList( mVariables, keepDisabled );
}

pEnvironmentVariablesModel::Variable pEnvironmentVariablesModel::variable( const QString& name ) const
{
    return mVariables.value( name );
}

bool pEnvironmentVariablesModel::contains( const QString& variable ) const
{
    return mVariables.contains( variable );
}

bool pEnvironmentVariablesModel::isEmpty() const
{
    return mVariables.isEmpty();
}

pEnvironmentVariablesModel::Variables pEnvironmentVariablesModel::stringListToVariables( const QStringList& variables )
{
    pEnvironmentVariablesModel::Variables items;

    foreach ( const QString& variable, variables )
    {
        const QString name = variable.section( '=', 0, 0 );
        const QString value = variable.section( '=', 1 );

        pEnvironmentVariablesModel::Variable item;
        item.name = name;
        item.value = value;
        item.enabled = true;

        items[ name ] = item;
    }
    
    return items;
}

QStringList pEnvironmentVariablesModel::variablesToStringList( const pEnvironmentVariablesModel::Variables& variables, bool keepDisabled )
{
    QStringList items;
    
    foreach ( const pEnvironmentVariablesModel::Variable& variable, variables.values() )
    {
        if ( !variable.enabled && !keepDisabled )
        {
            continue;
        }
        
        items << QString( "%1=%2" ).arg( variable.name ).arg( variable.value );
    }
    
    return items;
}

void pEnvironmentVariablesModel::setVariables( const pEnvironmentVariablesModel::Variables& variables, bool setDefault )
{
    emit layoutAboutToBeChanged();

    int count = rowCount();

    if ( count > 0 )
    {
        beginRemoveRows( QModelIndex(), 0, count -1 );
    }

    mRowCount = 0;
    mOrder.clear();
    mVariables.clear();

    if ( setDefault )
    {
        mDefaultVariables.clear();
    }

    if ( count > 0 )
    {
        endRemoveRows();
    }

    count = variables.count();

    if ( count != 0 )
    {
        beginInsertRows( QModelIndex(), 0, count -1 );
    }

    mVariables = variables;
    mRowCount = count;

    if ( setDefault )
    {
        setDefaultVariables( mVariables );
    }

    QStringList keys = mVariables.keys();
    qSort( keys );

    foreach ( const QString& key, keys )
    {
        mOrder << &mVariables[ key ];
    }

    if ( count > 0 )
    {
        endInsertRows();
    }

    emit layoutChanged();
}

void pEnvironmentVariablesModel::setDefaultVariables( const pEnvironmentVariablesModel::Variables& variables )
{
    mDefaultVariables = variables;
    emit defaultVariablesChanged();
}

void pEnvironmentVariablesModel::setVariables( const QStringList& variables, bool setDefault )
{
    setVariables( stringListToVariables( variables ), setDefault );
}

void pEnvironmentVariablesModel::setVariable( const QString& name, const pEnvironmentVariablesModel::Variable& variable )
{
    const bool hasVariable = mVariables.contains( name );
    QStringList keys = mVariables.keys();
    int row = -1;

    if ( !hasVariable )
    {
        keys << name;
        qSort( keys );
        row = keys.indexOf( name );

        beginInsertRows( QModelIndex(), row, row );
    }

    mVariables[ name ] = variable;

    if ( !hasVariable )
    {
        mOrder.insert( row, &mVariables[ name ] );
        mRowCount++;

        endInsertRows();
    }
}

void pEnvironmentVariablesModel::removeVariable( const QString& name )
{
    if ( !mVariables.contains( name ) )
    {
        return;
    }

    pEnvironmentVariablesModel::Variable& variable = mVariables[ name ];
    const int row = mOrder.indexOf( &variable );

    beginRemoveRows( QModelIndex(), row, row );
    mRowCount--;
    mOrder.removeAt( row );
    mVariables.remove( name );
    endRemoveRows();
}

void pEnvironmentVariablesModel::clearVariables()
{
    setVariables( pEnvironmentVariablesModel::Variables(), false );
}

void pEnvironmentVariablesModel::resetVariablesToDefault()
{
    setVariables( pEnvironmentVariablesModel::Variables( mDefaultVariables ), false );
}

void pEnvironmentVariablesModel::resetVariablesToSystem( bool setDefault )
{
    setVariables( QProcess::systemEnvironment(), setDefault );
}