1 /* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
2  * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
3 
4 #ifndef __INSTANCES_HH_INCLUDED__
5 #define __INSTANCES_HH_INCLUDED__
6 
7 #include "config.hh"
8 #include "dictionary.hh"
9 #include <QIcon>
10 #include <limits.h>
11 
12 // This complements Config, providing instances for the stored configurations.
13 // Simply put, it can convert groups to ones which hold references to
14 // dictionaries directly, instead of only having their ids, and can also convert
15 // them back.
16 
17 namespace Instances {
18 
19 using std::vector;
20 
21 struct Group
22 {
23   unsigned id;
24   QString name, icon, favoritesFolder;
25   QIcon iconData;
26   QKeySequence shortcut;
27   vector< sptr< Dictionary::Class > > dictionaries;
28 
29   /// Instantiates the given group from its configuration. If some dictionary
30   /// wasn't found, it just skips it.
31   Group( Config::Group const & cfgGroup,
32          vector< sptr< Dictionary::Class > > const & allDictionaries,
33          Config::Group const & inactiveGroup );
34 
35   /// Creates an empty group.
36   Group( QString const & name_ );
37 
38   /// Makes the configuration group from the current contents.
39   Config::Group makeConfigGroup();
40 
41   /// Makes an icon object for this group, based on the icon's name or iconData.
42   QIcon makeIcon() const;
43 
44   /// Remove id's if not presented in group dictionaries
45   void checkMutedDictionaries( Config::MutedDictionaries * mutedDictionaries ) const;
46 
47   // Some constants
48 
49   /// The id of the 'All' group
50   static const unsigned AllGroupId = UINT_MAX - 1;
51 
52   /// The id of the fictious 'Help' group
53   static const unsigned HelpGroupId = UINT_MAX;
54 
55   /// Invalid value, used to specify that no group id is specified at all.
56   static const unsigned NoGroupId = 0;
57 };
58 
59 struct Groups: public vector< Group >
60 {
61   /// Tries finding the given group by its id. Returns the group found, or
62   /// 0 if there's no such group.
63   Group * findGroup( unsigned id );
64   Group const * findGroup( unsigned id ) const;
65 };
66 
67 /// Adds any dictionaries not already present in the given group or in
68 /// inactiveDictionaires to its end. Meant to be used with dictionaryOrder
69 /// special group.
70 void complementDictionaryOrder( Group & dictionaryOrder,
71                                 Group const & inactiveDictionaries,
72                                 vector< sptr< Dictionary::Class > > const &
73                                 allDictionaries );
74 
75 /// For any dictionaries present in the group, updates their names to match
76 /// the dictionaries they refer to in their current form, if they exist.
77 /// If the dictionary instance can't be located, the name is left untouched.
78 void updateNames( Config::Group &,
79                   vector< sptr< Dictionary::Class > > const & allDictionaries );
80 
81 /// Does updateNames() for a set of given groups.
82 void updateNames( Config::Groups &,
83                   vector< sptr< Dictionary::Class > > const & allDictionaries );
84 
85 /// Does updateNames() for any relevant dictionary groups present in the
86 /// configuration.
87 void updateNames( Config::Class &,
88                   vector< sptr< Dictionary::Class > > const & allDictionaries );
89 
90 /// Creates icon from icon data. Used by Group, but also by others who work
91 /// with icon data directly.
92 QIcon iconFromData( QByteArray const & );
93 
94 }
95 
96 #endif
97