~ubuntu-branches/ubuntu/precise/kbibtex/precise

« back to all changes in this revision

Viewing changes to src/libkbibtexio/config/bibtexentries.cpp

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2011-07-18 09:29:48 UTC
  • mfrom: (1.1.6) (2.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20110718092948-ksxjmg7kdfamolmg
Tags: 0.3-1
* First upstream release for KDE4 (Closes: #634255). A number of search
  engines are still missing, in comparison to the 0.2 series.
* Bumped Standards-Version to 3.9.2, no changes necessary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
*   Copyright (C) 2004-2010 by Thomas Fischer                             *
 
3
*   fischer@unix-ag.uni-kl.de                                             *
 
4
*                                                                         *
 
5
*   This program is free software; you can redistribute it and/or modify  *
 
6
*   it under the terms of the GNU General Public License as published by  *
 
7
*   the Free Software Foundation; either version 2 of the License, or     *
 
8
*   (at your option) any later version.                                   *
 
9
*                                                                         *
 
10
*   This program is distributed in the hope that it will be useful,       *
 
11
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
*   GNU General Public License for more details.                          *
 
14
*                                                                         *
 
15
*   You should have received a copy of the GNU General Public License     *
 
16
*   along with this program; if not, write to the                         *
 
17
*   Free Software Foundation, Inc.,                                       *
 
18
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 
19
***************************************************************************/
 
20
 
 
21
#include <KGlobal>
 
22
#include <KStandardDirs>
 
23
#include <KSharedConfig>
 
24
#include <KConfigGroup>
 
25
#include <KSharedPtr>
 
26
#include <KDebug>
 
27
 
 
28
#include <entry.h>
 
29
#include "bibtexentries.h"
 
30
 
 
31
static const int BibTeXEntriesMax = 256;
 
32
 
 
33
class BibTeXEntries::BibTeXEntriesPrivate
 
34
{
 
35
public:
 
36
    BibTeXEntries *p;
 
37
 
 
38
    KConfig *systemDefaultsConfig;
 
39
    KSharedConfigPtr userConfig;
 
40
 
 
41
    static BibTeXEntries *singleton;
 
42
 
 
43
    BibTeXEntriesPrivate(BibTeXEntries *parent)
 
44
            : p(parent) {
 
45
        systemDefaultsConfig = new KConfig(KStandardDirs::locate("appdata", "entrytypes.rc"), KConfig::SimpleConfig);
 
46
        userConfig = KSharedConfig::openConfig(KStandardDirs::locateLocal("appdata", "entrytypes.rc"), KConfig::SimpleConfig);
 
47
    }
 
48
 
 
49
    ~BibTeXEntriesPrivate() {
 
50
        delete systemDefaultsConfig;
 
51
    }
 
52
 
 
53
    void load() {
 
54
// TODO
 
55
        p->clear();
 
56
 
 
57
        // TODO: Dummy implementation
 
58
        EntryDescription ed;
 
59
 
 
60
        for (int col = 1; col < BibTeXEntriesMax; ++col) {
 
61
            QString groupName = QString("EntryType%1").arg(col);
 
62
            KConfigGroup usercg(userConfig, groupName);
 
63
            KConfigGroup systemcg(systemDefaultsConfig, groupName);
 
64
 
 
65
            ed.upperCamelCase = systemcg.readEntry("UpperCamelCase", "");
 
66
            ed.upperCamelCase = usercg.readEntry("UpperCamelCase", ed.upperCamelCase);
 
67
            if (ed.upperCamelCase.isEmpty()) continue;
 
68
            ed.upperCamelCaseAlt = systemcg.readEntry("UpperCamelCaseAlt", "");
 
69
            ed.upperCamelCaseAlt = usercg.readEntry("UpperCamelCaseAlt", ed.upperCamelCaseAlt);
 
70
            ed.label = systemcg.readEntry("Label", ed.upperCamelCase);;
 
71
            ed.label = usercg.readEntry("Label", ed.label);;
 
72
            p->append(ed);
 
73
        }
 
74
    }
 
75
};
 
76
 
 
77
BibTeXEntries *BibTeXEntries::BibTeXEntriesPrivate::singleton = NULL;
 
78
 
 
79
 
 
80
BibTeXEntries::BibTeXEntries()
 
81
        : d(new BibTeXEntriesPrivate(this))
 
82
{
 
83
    d->load();
 
84
}
 
85
 
 
86
BibTeXEntries::~BibTeXEntries()
 
87
{
 
88
    delete d;
 
89
}
 
90
 
 
91
BibTeXEntries* BibTeXEntries::self()
 
92
{
 
93
    if (BibTeXEntriesPrivate::singleton == NULL)
 
94
        BibTeXEntriesPrivate::singleton = new BibTeXEntries();
 
95
    return BibTeXEntriesPrivate::singleton;
 
96
}
 
97
 
 
98
QString BibTeXEntries::format(const QString& name, KBibTeX::Casing casing) const
 
99
{
 
100
    QString iName = name.toLower();
 
101
 
 
102
    switch (casing) {
 
103
    case KBibTeX::cLowerCase: return iName;
 
104
    case KBibTeX::cUpperCase: return name.toUpper();
 
105
    case KBibTeX::cInitialCapital:
 
106
        iName[0] = iName[0].toUpper();
 
107
        return iName;
 
108
    case KBibTeX::cLowerCamelCase: {
 
109
        for (ConstIterator it = begin(); it != end(); ++it) {
 
110
            /// configuration file uses camel-case
 
111
            QString itName = (*it).upperCamelCase.toLower();
 
112
            if (itName == iName && (*it).upperCamelCase == QString::null) {
 
113
                iName = (*it).upperCamelCase;
 
114
                break;
 
115
            }
 
116
        }
 
117
 
 
118
        /// make an educated guess how camel-case would look like
 
119
        iName[0] = iName[0].toLower();
 
120
        return iName;
 
121
    }
 
122
    case KBibTeX::cUpperCamelCase: {
 
123
        for (ConstIterator it = begin(); it != end(); ++it) {
 
124
            /// configuration file uses camel-case
 
125
            QString itName = (*it).upperCamelCase.toLower();
 
126
            if (itName == iName && (*it).upperCamelCase == QString::null) {
 
127
                iName = (*it).upperCamelCase;
 
128
                break;
 
129
            }
 
130
        }
 
131
 
 
132
        /// make an educated guess how camel-case would look like
 
133
        iName[0] = iName[0].toUpper();
 
134
        return iName;
 
135
    }
 
136
    }
 
137
    return name;
 
138
}
 
139
 
 
140
QString BibTeXEntries::label(const QString& name) const
 
141
{
 
142
    const QString iName = name.toLower();
 
143
 
 
144
    for (ConstIterator it = begin(); it != end(); ++it) {
 
145
        /// configuration file uses camel-case
 
146
        QString itName = (*it).upperCamelCase.toLower();
 
147
        if (itName == iName || (!(itName = (*it).upperCamelCaseAlt.toLower()).isEmpty() && itName == iName))
 
148
            return (*it).label;
 
149
    }
 
150
    return QString::null;
 
151
}