1 /*
2  * Copyright 2013 Canonical Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authors: Lars Uebernickel <lars.uebernickel@canonical.com>
17  *          Ryan Lortie <desrt@desrt.ca>
18  */
19 
20 #include <glib.h>
21 #include <QString>
22 
23 /* convert 'some-key' to 'someKey' or 'SomeKey'.
24  * the second form is needed for appending to 'set' for 'setSomeKey'
25  */
qtify_name(const char * name)26 QString qtify_name(const char *name)
27 {
28     bool next_cap = false;
29     QString result;
30 
31     while (*name) {
32         if (*name == '-') {
33             next_cap = true;
34         } else if (next_cap) {
35             result.append(QChar(*name).toUpper().toLatin1());
36             next_cap = false;
37         } else {
38             result.append(*name);
39         }
40 
41         name++;
42     }
43 
44     return result;
45 }
46 
47 /* Convert 'someKey' to 'some-key'
48  *
49  * This is the inverse function of qtify_name, iff qtify_name was called with a
50  * valid gsettings key name (no capital letters, no consecutive dashes).
51  *
52  * Returns a newly-allocated string.
53  */
unqtify_name(const QString & name)54 gchar * unqtify_name(const QString &name)
55 {
56     const gchar *p;
57     QByteArray bytes;
58     GString *str;
59 
60     bytes = name.toUtf8();
61     str = g_string_new (nullptr);
62 
63     for (p = bytes.constData(); *p; p++) {
64         const QChar c(*p);
65         if (c.isUpper()) {
66             g_string_append_c (str, '-');
67             g_string_append_c (str, c.toLower().toLatin1());
68         }
69         else {
70             g_string_append_c (str, *p);
71         }
72     }
73 
74     return g_string_free(str, FALSE);
75 }
76