summaryrefslogtreecommitdiffstats
path: root/kcontrol/kfontinst/kfontinst/Main.cpp
blob: a2aa5766b9a1da4653f1883184005dce6bb91a41 (plain)
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
////////////////////////////////////////////////////////////////////////////////
//
// File Name     : Main.cpp
// Author        : Craig Drummond
// Project       : K Font Installer
// Creation Date : 20/03/2003
// Version       : $Revision$ $Date$
//
////////////////////////////////////////////////////////////////////////////////
//
// 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 Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
////////////////////////////////////////////////////////////////////////////////
// (C) Craig Drummond, 2003, 2004
////////////////////////////////////////////////////////////////////////////////

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "Misc.h"
#include "FontEngine.h"
#include "Fontmap.h"
#include "XConfig.h"
#include "kxftconfig.h"
#include <fontconfig/fontconfig.h>
#include <tqfile.h>
#include <stdio.h>

//
// Bug#99335 Solaris 2.6 does not have getopt.h :-(
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
#include <unistd.h>
#endif
#include <iostream>

#define KFI_XF86CFG "XF86Config"
#define KFI_XORGCFG "xorg.conf"

static const char * getFile(const char *entry, const char **posibilities)
{
    if(KFI::Misc::fExists(entry))
        return entry;
    else
    {
        int f;

        for(f=0; posibilities[f]; ++f)
            if(KFI::Misc::fExists(posibilities[f]))
                break;

        return posibilities[f];
    }
}

static const char * constXConfigFiles[]=
{   
    "/etc/X11/"KFI_XORGCFG,
    "/etc/X11/"KFI_XORGCFG"-4",
    "/etc/"KFI_XORGCFG,
    "/usr/X11R6/etc/X11/"KFI_XORGCFG,
    "/usr/X11R6/etc/X11/"KFI_XORGCFG"-4",
    "/usr/X11R6/lib/X11/"KFI_XORGCFG,
    "/usr/X11R6/lib/X11/"KFI_XORGCFG"-4",

    "/etc/X11/"KFI_XF86CFG"-4",
    "/etc/X11/"KFI_XF86CFG,
    "/etc/"KFI_XF86CFG"-4",
    "/etc/"KFI_XF86CFG,
    "/usr/X11R6/etc/X11/"KFI_XF86CFG"-4",
    "/usr/X11R6/etc/X11/"KFI_XF86CFG,
    "/usr/X11R6/lib/X11/"KFI_XF86CFG"-4",
    "/usr/X11R6/lib/X11/"KFI_XF86CFG,

    NULL
};  

static const char * constXfsConfigFiles[]=
{
    "/etc/X11/fs/config",
    "/usr/openwin/lib/X11/fonts/fontserver.cfg",
    NULL
};

KFI::CXConfig * getXCfg(bool root)
{
    if(root)
    {
        //
        // Try to determine location for X and xfs config files...
        // ...note on some systems (Solaris and HP-UX) only the xfs file will be found
        bool          xfs=false;
        KFI::CXConfig *xcfg=NULL;
        TQString       xConfigFile=getFile(TQFile::encodeName(constXConfigFiles[0]), constXConfigFiles),
                      xfsConfigFile=getFile(TQFile::encodeName(constXfsConfigFiles[0]), constXfsConfigFiles);
            
        // If found xfs, but not X - then assume that xfs is being used...
        if(!xfsConfigFile.isEmpty() && xConfigFile.isEmpty())
            xfs=true;
        else if(!xConfigFile.isEmpty()) // Read xConfig file to determine which one...
        {
            xcfg=new KFI::CXConfig(KFI::CXConfig::X11, xConfigFile);

            if(!xfsConfigFile.isEmpty() && xcfg->xfsInPath())
            {
                delete xcfg;
                xfs=true;
            }
        }

        // OK, if still set to X11 config, but this mentions fontconfig FPE, then delete - as we're not interested
        // anymore...
        if(xcfg && xcfg->fcInPath())
            delete xcfg;

        return xfs ? new KFI::CXConfig(KFI::CXConfig::XFS, xfsConfigFile) : xcfg;
    }

    return NULL;
}

static void usage(char *app)
{
    std::cerr << "Usage: " << app << " [OPTIONS]... [FOLDER]..." << std::endl
              << std::endl
              << "  Helper application for KDE's fonts:/ ioslave." << std::endl
              << std::endl
#ifdef HAVE_GETOPT_H
              << "  -x, --configure_x        Configure FOLDER for regular x - i.e." << std::endl
              << "                           create fonts.dir, fonts.scale and encodngs.dir" << std::endl
              << std::endl
              << "  -g, --configure_gs       Create Fontmap file. If run as root, then " << std::endl
              << "                           no paramter is required as all fonts are " << std::endl
              << "                           configured, and Fontmap placed in /etc/fonts" << std::endl
              << "                           For non-root, fonts located in FOLDER are" << std::endl
              << "                           configured, and Fontmap placed there." << std::endl
              << std::endl
              << "  -f, --add_to_fc_cfg      Add FOLDER to fontconfig config files." << std::endl
              << std::endl
              << "  -a, --add_to_x_cfg       Add FOLDER to X config files only when run as root.," << std::endl
              << std::endl
              << "  -r, --refresh_x          Refresh X." << std::endl
              << std::endl
              << "  -s, --refresh_xfs        Refresh Xfs." << std::endl
#else
              << "  -x                       Configure FOLDER for regular x - i.e." << std::endl
              << "                           create fonts.dir, fonts.scale and encodngs.dir" << std::endl
              << std::endl
              << "  -g                       Create Fontmap file. If run as root, then " << std::endl
              << "                           no paramter is required as all fonts are " << std::endl
              << "                           configured, and Fontmap placed in /etc/fonts" << std::endl
              << "                           For non-root, fonts located in FOLDER are" << std::endl
              << "                           configured, and Fontmap placed there." << std::endl
              << std::endl
              << "  -f                       Add FOLDER to fontconfig config files." << std::endl
              << std::endl
              << "  -a                       Add FOLDER to X config files only when run as root.," << std::endl
              << std::endl
              << "  -r                       Refresh X." << std::endl
              << std::endl
              << "  -s                       Refresh Xfs." << std::endl
#endif
              << std::endl
              << std::endl
              << "  (C) Craig Drummond, 2003, 2004." << std::endl
              << std::endl;

    exit(-1);
}

void refresh(bool refreshX, bool refreshXfs, bool root)
{
    if(refreshX)
        KFI::CXConfig::refreshPaths(false);
    if(refreshXfs && root)
        KFI::CXConfig::refreshPaths(true);
}

int main(int argc, char *argv[])
{
#ifdef HAVE_GETOPT_H
    static struct option options[]=
    {
        { "configure_x",    0, 0, 'x' },
        { "configure_gs",   0, 0, 'g' },
        { "add_to_fc_cfg",  0, 0, 'f' },
        { "add_to_x_cfg",   0, 0, 'a' },
        { "refresh_x",      0, 0, 'r' },
        { "refresh_xfs",    0, 0, 's' },
        { 0,                0, 0, 0   }
    };
#endif

    int  c=0,
         rv=0;
    bool doX=false,
         doGs=false,
         addToX=false,
         addToFc=false,
         refreshX=false,
         refreshXfs=false,
         root=KFI::Misc::root();

#ifdef HAVE_GETOPT_H
    int optIndex;
    while(-1!=(c=getopt_long(argc, argv, "xgfars", options, &optIndex)))
#else
    while(-1!=(c=getopt(argc, argv, "xgfars")))
#endif
        switch(c)
        {
            case 'x':
                doX=true;
                break;
            case 'g':
                doGs=true;
                break;
            case 'f':
                addToFc=true;
                break;
            case 'a':
                addToX=true;
                break;
            case 'r':
                refreshX=true;
                break;
            case 's':
                refreshXfs=true;
                break;
            case '?':
                usage(argv[0]);
                break;
        }

    int  left=argc-optind;
    bool folderRequired=doX || addToX || addToFc || (!root && doGs);

    if (left>1 || (0==left && folderRequired) || (!doX && !doGs && !addToX && !addToFc))
        usage(argv[0]);
    else
    {
        TQString folder;

        if(folderRequired)
        {
            folder=argv[optind];
            unsigned int len=folder.length();

            // Remove quotes...
            if( (folder[0]==TQChar('\'') || folder[0]==TQChar('\"')) &&
                (folder[len-1]==TQChar('\'') || folder[len-1]==TQChar('\"')))
                folder=folder.mid(1, len-2);
            folder=KFI::Misc::dirSyntax(folder);
        }

        if(folderRequired && !KFI::Misc::dExists(folder))
        {
            std::cerr << "ERROR: " << TQFile::encodeName(folder) << " does not exist!" << std::endl;
            rv=-2;
        }
        else
        {
            if(!folder.isEmpty())
            {
                if(0==rv && addToFc)
                {
                    //
                    // Only add folder to fontconfig's config if its not already there...
                    FcStrList *list=FcConfigGetFontDirs(FcConfigGetCurrent());
                    FcChar8   *dir;
                    bool      found=false;

                    while((dir=FcStrListNext(list)))
                        if(0==KFI::Misc::dirSyntax((const char *)dir).find(folder))
                            found=true;

                    if(!found)
                    {
                        KXftConfig *xft=new KXftConfig(KXftConfig::Dirs, root);

                        xft->addDir(folder);
                        rv=xft->apply() ? 0 : -3;
                        delete xft;
                   }
                }

                if(0==rv && addToX && root)
                {
                    KFI::CXConfig *x=NULL;

                    if((x=getXCfg(true)))
                    {
                        x->addPath(folder);
                        rv=x->writeConfig() ? 0 : -4;
                        delete x;
                    }
                    else
                        rv=-5;
                }
            }

            if(0==rv && (doX || doGs))
            {
                if(0==rv && doX)
                    rv=KFI::CXConfig::configureDir(folder) ? 0 : -5;

                refresh(refreshX, refreshXfs, root);

                if(0==rv && doGs)
                {
                    KFI::CFontEngine fe;
                    rv=KFI::Fontmap::create(root ? TQString::null : folder, fe) ? 0 : -6;
                }
            }
            else if(0==rv)
                refresh(refreshX, refreshXfs, root);
        }
    }

    return rv;
}