summaryrefslogtreecommitdiffstats
path: root/chalk/ui/kis_resourceserver.cc
blob: 3076bc00d958b4f3ed0d30e7ab504545ecf6dfe7 (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
/*
 *  kis_resourceserver.cc - part of KImageShop
 *
 *  Copyright (c) 1999 Matthias Elter <[email protected]>
 *  Copyright (c) 2003 Patrick Julien <[email protected]>
 *  Copyright (c) 2005 Sven Langkamp <[email protected]>
 *
 *  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.
 */
#include <tqfileinfo.h>
#include <tqstringlist.h>
#include <tqthread.h>
#include <tqdir.h>

#include <kdebug.h>
#include <tdeglobal.h>
#include <kstandarddirs.h>
#include <kinstance.h>

#include "kis_resource.h"
#include "kis_factory.h"
#include "kis_generic_registry.h"
#include "kis_resourceserver.h"
#include "kis_brush.h"
#include "kis_imagepipe_brush.h"
#include "kis_gradient.h"
#include "kis_pattern.h"
#include "kis_palette.h"
#include <kogradientmanager.h>

KisResourceServerBase::KisResourceServerBase(TQString type)
    : m_type(type), m_loaded(false)
{
}

KisResourceServerBase::~KisResourceServerBase()
{
}

void KisResourceServerBase::loadResources(TQStringList filenames)
{
    TQStringList uniqueFiles;

    while( !filenames.empty() )
    {

        TQString front = *filenames.begin();
        filenames.pop_front();

        TQString fname = TQFileInfo(front).fileName();
        //ebug() << "Loading " << fname << "\n";
        // XXX: Don't load resources with the same filename. Actually, we should look inside
        //      the resource to find out whether they are really the same, but for now this
        //      will prevent the same brush etc. showing up twice.
        if (uniqueFiles.empty() || uniqueFiles.find(fname) == uniqueFiles.end()) {
            uniqueFiles.append(fname);
            KisResource *resource;
            resource = createResource(front);
            if(resource->load() && resource->valid())
            {
                m_resources.append(resource);
                TQ_CHECK_PTR(resource);
                emit resourceAdded(resource);
            }
            else {
                delete resource;
            }
        }
    }
    m_loaded = true;
}

TQValueList<KisResource*> KisResourceServerBase::resources()
{
    if(!m_loaded) {
        return TQValueList<KisResource*>();
    }

    return m_resources;
}

void KisResourceServerBase::addResource(KisResource* resource)
{
    if (!resource->valid()) {
        kdWarning(41001) << "Tried to add an invalid resource!" << endl;
        return;
    }

    m_resources.append(resource);
    emit resourceAdded(resource);
}


class ResourceLoaderThread : public TQThread {

public:

    ResourceLoaderThread(KisResourceServerBase * server, TQStringList files)
        : TQThread()
        , m_server(server)
        , m_fileNames( files )
    {
    }


    void run()
    {
        m_server->loadResources(m_fileNames);
    }

private:

    KisResourceServerBase * m_server;
    TQStringList m_fileNames;

};

TQStringList getFileNames( TQString extensions, TQString type )
{
    TQStringList extensionList = TQStringList::split(":", extensions);
    TQStringList fileNames;

    TQStringList::Iterator it;
    for ( it = extensionList.begin(); it != extensionList.end(); ++it ) {
        TQString s = (*it);
        fileNames += KisFactory::instance()->dirs()->findAllResources(type.ascii(), (*it));
    }
    return fileNames;
}


KisResourceServerRegistry *KisResourceServerRegistry::m_singleton = 0;

KisResourceServerRegistry::KisResourceServerRegistry()
{

    KisResourceServer<KisBrush>* brushServer = new KisResourceServer<KisBrush>("kis_brushes");
    ResourceLoaderThread t1 (brushServer, getFileNames( "*.gbr","kis_brushes" ));
    t1.start();

    KisResourceServer<KisImagePipeBrush>* imagePipeBrushServer = new KisResourceServer<KisImagePipeBrush>("kis_brushes");
    ResourceLoaderThread t2 (imagePipeBrushServer, getFileNames( "*.gih", "kis_brushes"));
    t2.start();

    KisResourceServer<KisPattern>* patternServer = new KisResourceServer<KisPattern>("kis_patterns");
    ResourceLoaderThread t3 (patternServer, getFileNames("*.pat", "kis_patterns"));
    t3.start();

    KisResourceServer<KisGradient>* gradientServer = new KisResourceServer<KisGradient>("kis_gradients");
    ResourceLoaderThread t4 (gradientServer, getFileNames(KoGradientManager::filters().join( ":" ), "kis_gradients"));
    t4.start();


    KisResourceServer<KisPalette>* paletteServer = new KisResourceServer<KisPalette>("kis_palettes");
    ResourceLoaderThread t5 (paletteServer, getFileNames("*.gpl:*.pal:*.act", "kis_palettes") );
    t5.start();

    t1.wait();
    t2.wait();
    t3.wait();
    t4.wait();
    t5.wait();

    add( KisID( "BrushServer", ""), brushServer );
    add( KisID( "ImagePipeBrushServer", ""), imagePipeBrushServer );
    add( KisID( "PatternServer", ""), patternServer );
    add( KisID( "GradientServer", ""), gradientServer );
    add( KisID( "PaletteServer", ""), paletteServer );

}

KisResourceServerRegistry::~KisResourceServerRegistry()
{
}

KisResourceServerRegistry* KisResourceServerRegistry::instance()
{
     if(KisResourceServerRegistry::m_singleton == 0)
     {
         KisResourceServerRegistry::m_singleton = new KisResourceServerRegistry();
     }
    return KisResourceServerRegistry::m_singleton;
}


#include "kis_resourceserver.moc"