1 /*=============================================================================
2 
3   Library: CTK
4 
5   Copyright (c) German Cancer Research Center,
6     Division of Medical and Biological Informatics
7 
8   Licensed under the Apache License, Version 2.0 (the "License");
9   you may not use this file except in compliance with the License.
10   You may obtain a copy of the License at
11 
12     http://www.apache.org/licenses/LICENSE-2.0
13 
14   Unless required by applicable law or agreed to in writing, software
15   distributed under the License is distributed on an "AS IS" BASIS,
16   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   See the License for the specific language governing permissions and
18   limitations under the License.
19 
20 =============================================================================*/
21 
22 #include "ctkPluginArchiveSQL_p.h"
23 
24 #include "ctkPluginException.h"
25 #include "ctkPluginStorageSQL_p.h"
26 #include "ctkPluginDatabaseException.h"
27 
28 #include <QStringList>
29 #include <QFile>
30 
31 
32 //----------------------------------------------------------------------------
ctkPluginArchiveSQL(ctkPluginStorageSQL * pluginStorage,const QUrl & pluginLocation,const QString & localPluginPath,int pluginId,int startLevel,const QDateTime & lastModified,int autostartSetting)33 ctkPluginArchiveSQL::ctkPluginArchiveSQL(ctkPluginStorageSQL* pluginStorage,
34                                          const QUrl& pluginLocation, const QString& localPluginPath,
35                                          int pluginId, int startLevel, const QDateTime& lastModified,
36                                          int autostartSetting)
37   : key(-1), autostartSetting(autostartSetting), id(pluginId), generation(0)
38   , startLevel(startLevel), lastModified(lastModified), location(pluginLocation)
39   , localPluginPath(localPluginPath), storage(pluginStorage)
40 {
41 }
42 
43 //----------------------------------------------------------------------------
ctkPluginArchiveSQL(QSharedPointer<ctkPluginArchiveSQL> old,int generation,const QUrl & pluginLocation,const QString & localPluginPath)44 ctkPluginArchiveSQL::ctkPluginArchiveSQL(QSharedPointer<ctkPluginArchiveSQL> old, int generation,
45                                          const QUrl &pluginLocation, const QString &localPluginPath)
46   : key(-1), autostartSetting(old->autostartSetting), id(old->id), generation(generation)
47   , startLevel(0), location(pluginLocation), localPluginPath(localPluginPath)
48   , storage(old->storage)
49 {
50 }
51 
readManifest(const QByteArray & manifestResource)52 void ctkPluginArchiveSQL::readManifest(const QByteArray& manifestResource)
53 {
54   QByteArray manifestRes = manifestResource.isNull() ? this->getPluginResource("META-INF/MANIFEST.MF")
55                                                   : manifestResource;
56   if (manifestRes.isEmpty())
57   {
58     throw ctkPluginException(QString("ctkPlugin has no MANIFEST.MF resource, location=") + localPluginPath);
59   }
60 
61   manifest.read(manifestRes);
62 }
63 
64 //----------------------------------------------------------------------------
getAttribute(const QString & key) const65 QString ctkPluginArchiveSQL::getAttribute(const QString& key) const
66 {
67   return manifest.getAttribute(key);
68 }
69 
70 //----------------------------------------------------------------------------
getUnlocalizedAttributes() const71 QHash<QString,QString> ctkPluginArchiveSQL::getUnlocalizedAttributes() const
72 {
73   return manifest.getMainAttributes();
74 }
75 
getPluginGeneration() const76 int ctkPluginArchiveSQL::getPluginGeneration() const
77 {
78   return generation;
79 }
80 
81 //----------------------------------------------------------------------------
getPluginId() const82 int ctkPluginArchiveSQL::getPluginId() const
83 {
84   return id;
85 }
86 
87 //----------------------------------------------------------------------------
getPluginLocation() const88 QUrl ctkPluginArchiveSQL::getPluginLocation() const
89 {
90   return location;
91 }
92 
93 //----------------------------------------------------------------------------
getLibLocation() const94 QString ctkPluginArchiveSQL::getLibLocation() const
95 {
96   return localPluginPath;
97 }
98 
99 //----------------------------------------------------------------------------
getPluginResource(const QString & component) const100 QByteArray ctkPluginArchiveSQL::getPluginResource(const QString& component) const
101 {
102   try
103   {
104     return storage->getPluginResource(key, component);
105   }
106   catch (const ctkPluginDatabaseException& exc)
107   {
108     qDebug() << QString("Getting plugin resource %1 failed:").arg(component) << exc;
109     return QByteArray();
110   }
111 }
112 
113 //----------------------------------------------------------------------------
findResourcesPath(const QString & path) const114 QStringList ctkPluginArchiveSQL::findResourcesPath(const QString& path) const
115 {
116   try
117   {
118     return storage->findResourcesPath(key, path);
119   }
120   catch (const ctkPluginDatabaseException& exc)
121   {
122     qDebug() << QString("Getting plugin resource paths for %1 failed:").arg(path) << exc;
123   }
124   return QStringList();
125 }
126 
127 //----------------------------------------------------------------------------
getStartLevel() const128 int ctkPluginArchiveSQL::getStartLevel() const
129 {
130   return startLevel;
131 }
132 
133 //----------------------------------------------------------------------------
setStartLevel(int level)134 void ctkPluginArchiveSQL::setStartLevel(int level)
135 {
136   if (startLevel != level)
137   {
138     startLevel = level;
139     storage->setStartLevel(key, level);
140   }
141 }
142 
143 //----------------------------------------------------------------------------
getLastModified() const144 QDateTime ctkPluginArchiveSQL::getLastModified() const
145 {
146   return lastModified;
147 }
148 
149 //----------------------------------------------------------------------------
setLastModified(const QDateTime & dateTime)150 void ctkPluginArchiveSQL::setLastModified(const QDateTime& dateTime)
151 {
152   lastModified = dateTime;
153   storage->setLastModified(key, dateTime);
154 }
155 
156 //----------------------------------------------------------------------------
getAutostartSetting() const157 int ctkPluginArchiveSQL::getAutostartSetting() const
158 {
159   return autostartSetting;
160 }
161 
162 //----------------------------------------------------------------------------
setAutostartSetting(int setting)163 void ctkPluginArchiveSQL::setAutostartSetting(int setting)
164 {
165   if (autostartSetting != setting)
166   {
167     autostartSetting = setting;
168     storage->setAutostartSetting(key, setting);
169   }
170 }
171 
172 //----------------------------------------------------------------------------
purge()173 void ctkPluginArchiveSQL::purge()
174 {
175   storage->removeArchive(this);
176 }
177 
178 //----------------------------------------------------------------------------
close()179 void ctkPluginArchiveSQL::close()
180 {
181 
182 }
183