File: FileRecord.h

package info (click to toggle)
qtop 2.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,976 kB
  • sloc: cpp: 40,477; makefile: 7
file content (376 lines) | stat: -rw-r--r-- 8,703 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
365
366
367
368
369
370
371
372
373
374
375
376
#ifndef FileRecord_h
#define FileRecord_h

/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This 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 software 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, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

#include "Counter.h"
#include "Debug.h"
#include "File.h"
#include "QOrderedSet.h"
#include "TimeStamp.h"

#include <QList>
#include <QHash>
#include <QStringList>

//* handles previously opened file and tags
class FileRecord: private Base::Counter<FileRecord>
{

    public:

    //* shortcut to list of records
    using Set = QOrderedSet<FileRecord>;
    using List = QList<FileRecord>;
    using ListIterator = QListIterator<FileRecord>;

    //* mimetype for drag and drop operations
    static const QString MimeType;

    //* constructor
    explicit FileRecord( const File& file = File(), const TimeStamp& time = TimeStamp::now() ):
        Counter( "FileRecord" ),
        file_( file ),
        time_( time ),
        valid_( true )
    {}

    //* constructor
    explicit FileRecord( File&& file, const TimeStamp& time = TimeStamp::now() ):
        Counter( "FileRecord" ),
        file_( std::move(file) ),
        time_( time ),
        valid_( true )
    {}

    //* destructor
    virtual ~FileRecord() = default;

    //*@name accessors
    //@{

    //* file
    const File& file() const { return file_; }

    //* time stamp
    const TimeStamp& time() const
    { return time_; }

    //* flags
    int flags() const
    { return flags_; }

    //* flags
    bool hasFlag( int flag ) const
    { return flags_ & flag; }

    //* validity
    bool isValid() const
    { return valid_; }

    //* map string to property ID
    class PropertyId
    {

        public:

        using Id = quint32;

        //* get id matching name
        /** insert in map if name is new */
        static Id get( QString );

        //* get name matching id
        /** throw exception if not found */
        static QString get( Id );

        private:

        //* counter
        static Id& _counter();

        //* id map
        using IdMap = QHash< QString, Id >;

        //* id map
        static IdMap& _idMap();

        //* id map
        using NameMap = QStringList;

        //* name map
        static NameMap& _nameMap();

    };

    //* true if property is available
    bool hasProperty( QString tag ) const
    { return hasProperty( PropertyId::get( tag ) ); }

    //* true if property is available
    bool hasProperty( PropertyId::Id id ) const
    { return properties_.find( id ) != properties_.end(); }

    //* additional property map
    using PropertyMap = QHash< PropertyId::Id, QString >;

    //* property map
    const PropertyMap& properties() const
    { return properties_; }

    //* retrieve property
    QString property( QString tag ) const
    { return property( PropertyId::get( tag ) ); }

    //* retrieve property
    QString property( PropertyId::Id id ) const
    {
        PropertyMap::const_iterator iter(  properties_.find( id ) );
        return ( iter == properties_.end() ) ? "":iter.value();
    }

    //@}

    //*@name modifiers
    //@{

    //* file
    FileRecord& setFile( const File& file )
    {
        file_ = file;
        return *this;
    }

    //* time stamp
    FileRecord& setTime( const TimeStamp& time )
    {
        time_ = time;
        return *this;
    }

    //* flags
    FileRecord& setFlags( int value )
    {
        flags_ = value;
        return *this;
    }

    //* flags
    FileRecord& setFlag( int flag, bool value = true )
    {
        if( value ) { flags_ |= flag; }
        else { flags_ &= (~flag); }
        return *this;
    }

    //* validity
    FileRecord& setValid( bool valid )
    {
        valid_ = valid;
        return *this;
    }

    //* add property
    FileRecord& addProperty( QString tag, QString value )
    { return addProperty( PropertyId::get( tag ), value ); }

    //* add property
    FileRecord& addProperty( PropertyId::Id, QString );

    //@}

    //* used to retrieve file records that match a given flag
    class HasFlagFTor
    {

        public:

        //* constructor
        explicit HasFlagFTor( int flag ):
            flag_( flag )
         {}

        //* predicate
        bool operator() ( const FileRecord& record ) const
        { return record.hasFlag( flag_ ); }

        private:

        // predicted flag
        int flag_;

    };

    //* used to sort records according to files
    class FileFTor
    {

        public:

        //* predicate
        bool operator() (const FileRecord& first, const FileRecord& second) const
        { return first.file() < second.file(); }

    };

    //* used to retrieve FileRecord with identical filenames
    class SameFileFTor
    {

        public:

        //* constructor
        explicit SameFileFTor( const File& file = File("") ):
            file_( file )
        {}

        //* constructor
        explicit SameFileFTor( const FileRecord& record ):
            file_( record.file() )
        {}

        //* predicate
        bool operator() (const FileRecord& record ) const
        { return record.file() == file_; }

        //* predicate
        bool operator() (const FileRecord& first, const FileRecord& second) const
        { return first.file() == second.file(); }

        private:

        //* filename
        File file_;

    };

    //* used to sort FileRecords using canonical filenames
    class CanonicalFileFTor
    {

        public:

        //* predicate
        bool operator() (const FileRecord& first, const FileRecord& second) const
        { return first.file().canonicalName() < second.file().canonicalName(); }


    };

    //* used to remove FileRecord with identical canonical filenames
    class SameCanonicalFileFTor
    {

        public:

        //* constructor
        explicit SameCanonicalFileFTor( const File& file = File("") ):
            file_( file.canonicalName() )
        {}

        //* constructor
        explicit SameCanonicalFileFTor( const FileRecord& record ):
            file_( record.file().canonicalName() )
        {}

        //* predicate
        bool operator() (const FileRecord& first, const FileRecord& second) const
        { return first.file().canonicalName() == second.file().canonicalName(); }

        //* predicate
        bool operator() (const FileRecord& record ) const
        { return record.file().canonicalName() == file_; }

        private:

        //* filename
        File file_;

    };

    //* used to remove non-existing files
    class InvalidFTor
    {
        public:

        //* predicate
        bool operator()( const FileRecord& record )
        { return !record.isValid(); }

    };

    //* used to remove non-existing files
    class ValidFTor
    {
        public:

        //* predicate
        bool operator()( const FileRecord& record )
        { return record.isValid(); }

    };

    //* used to retrieve most recent file records
    class FirstOpenFTor
    {

        public:

        //* predicate
        bool operator() (const FileRecord& first, const FileRecord& second) const
        { return first.time() < second.time(); }

    };

    private:

    //* file
    File file_;

    //* additional properties
    PropertyMap properties_;

    //* time
    TimeStamp time_;

    //* flags
    int flags_ = 0;

    //* file validity (true if file exists)
    bool valid_ = false;

    //* streamers
    friend QTextStream& operator << ( QTextStream& out, const FileRecord& record )
    {
        out << record.file() << endl;
        for( PropertyMap::const_iterator iter = record.properties().begin(); iter != record.properties().end(); iter++ )
        { out << "  " << iter.key() << " (" << PropertyId::get( iter.key() ) << "): " << iter.value() << endl; }
        return out;
    }

    //* less than operator
    friend bool operator < (const FileRecord&, const FileRecord& );

    //* less than operator
    friend bool operator == (const FileRecord&, const FileRecord& );

};

#endif