1 // -*- c-basic-offset: 4 -*-
2 
3 /** @file PluginItems.cpp
4  *
5  *  @brief Reading python plugins metadata
6  *
7  *  @author Y. Levy, T. Modes
8  *
9  */
10 
11 /*
12  *  This program is free software; you can redistribute it and/or
13  *  modify it under the terms of the GNU General Public
14  *  License as published by the Free Software Foundation; either
15  *  version 2 of the License, or (at your option) any later version.
16  *
17  *  This software is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  *  General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public
23  *  License along with this software. If not, see
24  *  <http://www.gnu.org/licenses/>.
25  *
26  */
27 
28 #include <wx/wfstream.h>
29 #include <wx/txtstrm.h>
30 #include <hugin_version.h>
31 
32 //for natural sorting
33 #include "hugin_utils/alphanum.h"
34 #include "PluginItems.h"
35 
36 //for console/debugging output
37 #include <iostream>
38 
comparePluginItem(PluginItem item1,PluginItem item2)39 bool comparePluginItem(PluginItem item1,PluginItem item2)
40 {
41     int res=doj::alphanum_comp(
42         std::string(item1.GetCategory().mb_str(wxConvLocal)),
43         std::string(item2.GetCategory().mb_str(wxConvLocal)));
44     if(res<0)
45     {
46         return true;
47     }
48     else
49     {
50         if(res==0)
51         {
52             return (doj::alphanum_comp(
53                 std::string(item1.GetName().mb_str(wxConvLocal)),
54                 std::string(item2.GetName().mb_str(wxConvLocal)))<0);
55         }
56         else
57         {
58             return false;
59         };
60     };
61 };
62 
compareVersion(wxString v1,wxString v2)63 bool compareVersion(wxString v1, wxString v2)
64 {
65     return doj::alphanum_comp(std::string(v1.mb_str(wxConvLocal)),std::string(v2.mb_str(wxConvLocal))) < 0;
66 };
67 
PluginItem(wxFileName filename)68 PluginItem::PluginItem(wxFileName filename)
69 {
70     // default category if nothing else found
71     m_category = _("Miscellaneous");
72     // default plugin name if nothing else found is the file name
73     m_name=filename.GetFullName();
74     m_filename=filename;
75     m_description=wxT("");
76     m_validAPI=true;
77     ParseMetadata();
78 };
79 
ParseMetadata()80 void PluginItem::ParseMetadata()
81 {
82     // use wxWidgets to read the plugin files in search for meta data
83     wxFileInputStream in(m_filename.GetFullPath());
84     wxTextInputStream text(in);
85 
86     // read the plugin file and search if it contains meta data
87     bool foundCategory=false;
88     bool foundName=false;
89     bool foundAPImin=false;
90     bool foundAPImax=false;
91     bool foundSYS=false;
92     bool foundDescription=false;
93 #if defined __WXMSW__
94     wxString system(wxT("win"));
95 #elif defined __WXMAC__
96     wxString system(wxT("mac"));
97 #else
98     wxString system(wxT("nix"));
99 #endif
100     wxString tagSYS(wxT("@sys"));
101     wxString tagAPImin(wxT("@api-min"));
102     wxString tagAPImax(wxT("@api-max"));
103     wxString tagCategory(wxT("@category"));
104     wxString tagName(wxT("@name"));
105     wxString tagDescription(wxT("@description"));
106 
107     // tell me who you are processing
108     std::cout << m_filename.GetFullPath().mb_str(wxConvLocal) << std::endl;
109 
110 
111     while(!in.Eof() && !(foundCategory && foundName && foundAPImin && foundAPImax && foundSYS && foundDescription))
112     {
113         int pos;
114         wxString line=text.ReadLine();
115         //convert to lower case to make search for tag name case insensitive
116         wxString lowerLine=line.Lower();
117         pos=lowerLine.Find(tagSYS);
118         if(pos!=wxNOT_FOUND)
119         {
120             foundSYS=true;
121             pos=lowerLine.Find(system);
122             if(pos==wxNOT_FOUND)
123             {
124                 m_validAPI=false;
125                 std::cout << "   fails @sys" << std::endl;
126             };
127             continue;
128         };
129         pos=lowerLine.Find(tagAPImin);
130         if(pos!=wxNOT_FOUND)
131         {
132             foundAPImin=true;
133             wxString APImin = line.Mid(pos+1+tagAPImin.length()).Trim().Trim(false);
134             if(compareVersion(wxT(HUGIN_API_VERSION),APImin))
135             {
136                 m_validAPI=false;
137                 std::cout << "   fails @api-min" << std::endl;
138             };
139             continue;
140         };
141         pos=lowerLine.Find(tagAPImax);
142         if(pos!=wxNOT_FOUND)
143         {
144             foundAPImax=true;
145             wxString APImax = line.Mid(pos+1+tagAPImax.length()).Trim().Trim(false);
146             if(compareVersion(APImax,wxT(HUGIN_API_VERSION)))
147             {
148                 m_validAPI=false;
149                 std::cout << "   fails @api-max" << std::endl;
150             };
151             continue;
152         };
153         pos=lowerLine.Find(tagCategory);
154         if(pos!=wxNOT_FOUND)
155         {
156             m_category = line.Mid(pos+1+tagCategory.length()).Trim().Trim(false);
157             foundCategory=true;
158             std::cout << "   CAT:" << m_category.mb_str(wxConvLocal) << std::endl;
159             continue;
160         };
161         pos=lowerLine.Find(tagName);
162         if(pos!=wxNOT_FOUND)
163         {
164             m_name = line.Mid(pos+1+tagName.length()).Trim().Trim(false);
165             foundName=true;
166             std::cout << "   NAM:" << m_name.mb_str(wxConvLocal) << std::endl;
167             continue;
168         };
169         pos=lowerLine.Find(tagDescription);
170         if(pos!=wxNOT_FOUND)
171         {
172             m_description = line.Mid(pos+1+tagDescription.length()).Trim().Trim(false);
173             foundDescription=true;
174             continue;
175         };
176     };
177 };
178 
IsAPIValid() const179 const bool PluginItem::IsAPIValid() const
180 {
181     return m_validAPI;
182 };
183 
GetCategory() const184 const wxString PluginItem::GetCategory() const
185 {
186     return m_category;
187 };
188 
GetFilename() const189 const wxFileName PluginItem::GetFilename() const
190 {
191     return m_filename;
192 };
193 
GetName() const194 const wxString PluginItem::GetName() const
195 {
196     return m_name;
197 };
198 
GetDescription() const199 const wxString PluginItem::GetDescription() const
200 {
201     return m_description;
202 };
203