~ubuntu-branches/ubuntu/wily/psi/wily

« back to all changes in this revision

Viewing changes to third-party/qca/qca/src/qca_core.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2003-2007  Justin Karneges <justin@affinix.com>
 
2
 * Copyright (C) 2003-2008  Justin Karneges <justin@affinix.com>
3
3
 * Copyright (C) 2004,2005  Brad Hards <bradh@frogmouth.net>
4
4
 *
5
5
 * This library is free software; you can redistribute it and/or
14
14
 *
15
15
 * You should have received a copy of the GNU Lesser General Public
16
16
 * License along with this library; if not, write to the Free Software
17
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
18
 * 02110-1301  USA
18
19
 *
19
20
 */
20
21
 
60
61
public:
61
62
        int refs;
62
63
        bool secmem;
 
64
        bool loaded;
63
65
        bool first_scan;
64
66
        QString app_name;
65
67
        QMutex name_mutex;
72
74
        QMutex prop_mutex;
73
75
        QMap<QString,QVariantMap> config;
74
76
        QMutex config_mutex;
 
77
        QMutex logger_mutex;
75
78
 
76
79
        Global()
77
80
        {
78
81
                refs = 0;
79
82
                secmem = false;
 
83
                loaded = false;
80
84
                first_scan = false;
81
85
                rng = 0;
82
 
                logger = new Logger;
 
86
                logger = 0;
83
87
                manager = new ProviderManager;
84
88
        }
85
89
 
94
98
                logger = 0;
95
99
        }
96
100
 
97
 
        void ensure_first_scan()
 
101
        void ensure_loaded()
 
102
        {
 
103
                // probably we shouldn't overload scan mutex, or else rename it
 
104
                QMutexLocker locker(&scan_mutex);
 
105
                if(!loaded)
 
106
                {
 
107
                        loaded = true;
 
108
                        manager->setDefault(create_default_provider()); // manager owns it
 
109
                }
 
110
        }
 
111
 
 
112
        bool ensure_first_scan()
98
113
        {
99
114
                scan_mutex.lock();
100
115
                if(!first_scan)
102
117
                        first_scan = true;
103
118
                        manager->scan();
104
119
                        scan_mutex.unlock();
105
 
                        return;
 
120
                        return true;
106
121
                }
107
122
                scan_mutex.unlock();
 
123
                return false;
108
124
        }
109
125
 
110
126
        void scan()
119
135
        {
120
136
                KeyStoreManager::scan();
121
137
        }
 
138
 
 
139
        Logger *get_logger()
 
140
        {
 
141
                QMutexLocker locker(&logger_mutex);
 
142
                if(!logger)
 
143
                {
 
144
                        logger = new Logger;
 
145
 
 
146
                        // needed so deinit may delete the logger regardless
 
147
                        //   of what thread the logger was created from
 
148
                        logger->moveToThread(0);
 
149
                }
 
150
                return logger;
 
151
        }
 
152
 
 
153
        void unloadAllPlugins()
 
154
        {
 
155
                KeyStoreManager::shutdown();
 
156
 
 
157
                // if the global_rng was owned by a plugin, then delete it
 
158
                rng_mutex.lock();
 
159
                if(rng && (rng->provider() != manager->find("default")))
 
160
                {
 
161
                        delete rng;
 
162
                        rng = 0;
 
163
                }
 
164
                rng_mutex.unlock();
 
165
 
 
166
                manager->unloadAll();
 
167
        }
122
168
};
123
169
 
124
170
Q_GLOBAL_STATIC(QMutex, global_mutex)
180
226
        // plugins that have active objects (notably KeyStore).  we'll use a
181
227
        // post routine to force qca to deinit first.
182
228
        qAddPostRoutine(deinit);
183
 
 
184
 
        global->manager->setDefault(create_default_provider()); // manager owns it
185
229
}
186
230
 
187
231
void init()
211
255
        return true;
212
256
}
213
257
 
 
258
static bool global_check_load()
 
259
{
 
260
        Q_ASSERT(global);
 
261
        if(!global)
 
262
                return false;
 
263
        global->ensure_loaded();
 
264
        return true;
 
265
}
 
266
 
214
267
QMutex *global_random_mutex()
215
268
{
216
269
        return &global->rng_mutex;
233
286
 
234
287
bool haveSecureRandom()
235
288
{
236
 
        if(!global_check())
 
289
        if(!global_check_load())
237
290
                return false;
238
291
 
239
292
        QMutexLocker locker(global_random_mutex());
245
298
 
246
299
bool isSupported(const QStringList &features, const QString &provider)
247
300
{
248
 
        if(!global_check())
 
301
        if(!global_check_load())
249
302
                return false;
250
303
 
251
304
        // single
268
321
                if(features_have(global->manager->allFeatures(), features))
269
322
                        return true;
270
323
 
 
324
                global->manager->appendDiagnosticText(QString("Scanning to find features: %1\n").arg(features.join(" ")));
 
325
 
271
326
                // ok, try scanning for new stuff
272
327
                global->scan();
273
328
 
284
339
 
285
340
QStringList supportedFeatures()
286
341
{
287
 
        if(!global_check())
 
342
        if(!global_check_load())
288
343
                return QStringList();
289
344
 
290
345
        // query all features
294
349
 
295
350
QStringList defaultFeatures()
296
351
{
297
 
        if(!global_check())
 
352
        if(!global_check_load())
298
353
                return QStringList();
299
354
 
300
355
        return global->manager->find("default")->features();
302
357
 
303
358
ProviderList providers()
304
359
{
305
 
        if(!global_check())
 
360
        if(!global_check_load())
306
361
                return ProviderList();
307
362
 
308
363
        global->ensure_first_scan();
312
367
 
313
368
bool insertProvider(Provider *p, int priority)
314
369
{
315
 
        if(!global_check())
 
370
        if(!global_check_load())
316
371
                return false;
317
372
 
318
373
        global->ensure_first_scan();
322
377
 
323
378
void setProviderPriority(const QString &name, int priority)
324
379
{
325
 
        if(!global_check())
 
380
        if(!global_check_load())
326
381
                return;
327
382
 
328
383
        global->ensure_first_scan();
332
387
 
333
388
int providerPriority(const QString &name)
334
389
{
335
 
        if(!global_check())
 
390
        if(!global_check_load())
336
391
                return -1;
337
392
 
338
393
        global->ensure_first_scan();
342
397
 
343
398
Provider *findProvider(const QString &name)
344
399
{
345
 
        if(!global_check())
 
400
        if(!global_check_load())
346
401
                return 0;
347
402
 
348
403
        global->ensure_first_scan();
352
407
 
353
408
Provider *defaultProvider()
354
409
{
355
 
        if(!global_check())
 
410
        if(!global_check_load())
356
411
                return 0;
357
412
 
358
413
        return global->manager->find("default");
360
415
 
361
416
void scanForPlugins()
362
417
{
363
 
        if(!global_check())
 
418
        if(!global_check_load())
364
419
                return;
365
420
 
366
421
        global->scan();
369
424
 
370
425
void unloadAllPlugins()
371
426
{
372
 
        if(!global_check())
 
427
        if(!global_check_load())
373
428
                return;
374
429
 
375
 
        // if the global_rng was owned by a plugin, then delete it
376
 
        global->rng_mutex.lock();
377
 
        if(global->rng && (global->rng->provider() != global->manager->find("default")))
378
 
        {
379
 
                delete global->rng;
380
 
                global->rng = 0;
381
 
        }
382
 
        global->rng_mutex.unlock();
383
 
 
384
 
        global->manager->unloadAll();
 
430
        global->unloadAllPlugins();
385
431
}
386
432
 
387
433
QString pluginDiagnosticText()
388
434
{
389
 
        if(!global_check())
 
435
        if(!global_check_load())
390
436
                return QString();
391
437
 
392
438
        return global->manager->diagnosticText();
394
440
 
395
441
void clearPluginDiagnosticText()
396
442
{
397
 
        if(!global_check())
 
443
        if(!global_check_load())
398
444
                return;
399
445
 
400
446
        global->manager->clearDiagnosticText();
402
448
 
403
449
void appendPluginDiagnosticText(const QString &text)
404
450
{
405
 
        if(!global_check())
 
451
        if(!global_check_load())
406
452
                return;
407
453
 
408
454
        global->manager->appendDiagnosticText(text);
410
456
 
411
457
void setProperty(const QString &name, const QVariant &value)
412
458
{
413
 
        if(!global_check())
 
459
        if(!global_check_load())
414
460
                return;
415
461
 
416
462
        QMutexLocker locker(&global->prop_mutex);
420
466
 
421
467
QVariant getProperty(const QString &name)
422
468
{
423
 
        if(!global_check())
 
469
        if(!global_check_load())
424
470
                return QVariant();
425
471
 
426
472
        QMutexLocker locker(&global->prop_mutex);
454
500
        settings.beginGroup(name);
455
501
        QStringList keys = settings.childKeys();
456
502
        QVariantMap map;
457
 
        foreach(QString key, keys)
 
503
        foreach(const QString &key, keys)
458
504
                map[key] = settings.value(key);
459
505
        settings.endGroup();
460
506
 
493
539
 
494
540
void setProviderConfig(const QString &name, const QVariantMap &config)
495
541
{
496
 
        if(!global_check())
 
542
        if(!global_check_load())
497
543
                return;
498
544
 
499
545
        if(!configIsValid(config))
510
556
 
511
557
QVariantMap getProviderConfig(const QString &name)
512
558
{
513
 
        if(!global_check())
 
559
        if(!global_check_load())
514
560
                return QVariantMap();
515
561
 
516
562
        QVariantMap conf;
550
596
 
551
597
void saveProviderConfig(const QString &name)
552
598
{
553
 
        if(!global_check())
 
599
        if(!global_check_load())
554
600
                return;
555
601
 
556
602
        QMutexLocker locker(&global->config_mutex);
612
658
 
613
659
Logger *logger()
614
660
{
615
 
        return global->logger;
 
661
        return global->get_logger();
616
662
}
617
663
 
618
664
bool haveSystemStore()
696
742
static Provider *getProviderForType(const QString &type, const QString &provider)
697
743
{
698
744
        Provider *p = 0;
699
 
        bool scanned = false;
 
745
        bool scanned = global->ensure_first_scan();
700
746
        if(!provider.isEmpty())
701
747
        {
702
748
                // try using specific provider
703
749
                p = global->manager->findFor(provider, type);
704
 
                if(!p)
 
750
                if(!p && !scanned)
705
751
                {
706
752
                        // maybe this provider is new, so scan and try again
707
753
                        global->scan();
713
759
        {
714
760
                // try using some other provider
715
761
                p = global->manager->findFor(QString(), type);
716
 
                if((!p || p->name() == "default") && !scanned)
 
762
 
 
763
                // note: we used to rescan if no provider was found or if
 
764
                //   the only found provider was 'default'.  now we only
 
765
                //   rescan if no provider was found.  this optimizes lookups
 
766
                //   for features that are in the default provider (such as
 
767
                //   'sha1') when no other plugin is available.  the drawback
 
768
                //   is that if a plugin is installed later during runtime,
 
769
                //   then it won't be picked up without restarting the
 
770
                //   application or manually calling QCA::scanForPlugins.
 
771
                //if((!p || p->name() == "default") && !scanned)
 
772
                if(!p && !scanned)
717
773
                {
718
774
                        // maybe there are new providers, so scan and try again
719
775
                        //   before giving up or using default
733
789
 
734
790
Provider::Context *getContext(const QString &type, const QString &provider)
735
791
{
736
 
        if(!global_check())
 
792
        if(!global_check_load())
737
793
                return 0;
738
794
 
739
795
        Provider *p;
748
804
 
749
805
Provider::Context *getContext(const QString &type, Provider *_p)
750
806
{
751
 
        if(!global_check())
 
807
        if(!global_check_load())
752
808
                return 0;
753
809
 
754
810
        Provider *p;