Scroom  0.14
loader.cc File Reference
#include "loader.hh"
#include <cstdio>
#include <boost/filesystem.hpp>
#include <scroom/threadpool.hh>
#include "callbacks.hh"
#include "pluginmanager.hh"
Include dependency graph for loader.cc:

Classes

class  GObjectUnref< T >
 
class  GtkFileFilterInfoDeleter
 
class  GtkFileFilterListDestroyer
 
class  ScroomInterfaceImpl
 

Typedefs

using GtkFileFilterInfoPtr = std::unique_ptr< GtkFileFilterInfo, GtkFileFilterInfoDeleter >
 

Functions

void destroyGtkFileFilterList (std::list< GtkFileFilter * > &l)
 
char * charpFromString (const std::string &s)
 
GtkFileFilterInfoPtr filterInfoFromPath (const std::string &filename)
 
bool filterMatchesInfo (GtkFileFilterInfo const &info, std::list< GtkFileFilter * > const &filters)
 
void create (NewPresentationInterface *interface)
 
PresentationInterface::Ptr loadPresentation (const std::string &filename)
 
PresentationInterface::Ptr loadPresentation (GtkFileFilterInfo const &info)
 
PresentationInterface::Ptr loadPresentation (GtkFileFilterInfoPtr const &info)
 
void load (GtkFileFilterInfo const &info)
 
void load (GtkFileFilterInfoPtr const &info)
 
void load (const std::string &filename)
 

Typedef Documentation

◆ GtkFileFilterInfoPtr

using GtkFileFilterInfoPtr = std::unique_ptr<GtkFileFilterInfo, GtkFileFilterInfoDeleter>

Function Documentation

◆ charpFromString()

char* charpFromString ( const std::string &  s)
92 {
93  const size_t n = s.size();
94  std::unique_ptr<char[]> result(new char[n + 1]);
95  if(!result)
96  {
97  throw std::bad_alloc();
98  }
99 
100  strncpy(result.get(), s.c_str(), n + 1);
101  if(result[n] != 0)
102  {
103  throw std::length_error("String size changed during copying");
104  }
105 
106  return result.release();
107 }

Referenced by filterInfoFromPath().

Here is the caller graph for this function:

◆ create()

void create ( NewPresentationInterface interface)
176 {
177  PresentationInterface::Ptr const presentation = interface->createNew();
178  if(presentation)
179  {
180  on_presentation_created(presentation);
181  find_or_create_scroom(presentation);
182  }
183  else
184  {
185  throw std::invalid_argument("Unable to create requested presentation");
186  }
187 }

Referenced by BOOST_AUTO_TEST_CASE(), Operations1bpp::cache(), Operations8bpp::cache(), Operations24bpp::cache(), Operations::cache(), OperationsColormapped::cache(), Operations1bppClipped::cacheZoom(), on_new_activate(), anonymous_namespace{tiledbitmappresentation.cc}::OpenTiledBitmapAsPresentation::open(), TransparentOverlayViewInfo::redraw(), TEST(), and Scroom::TiledBitmap::ToOpenPresentationInterface().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ destroyGtkFileFilterList()

void destroyGtkFileFilterList ( std::list< GtkFileFilter * > &  l)
47 {
48  while(!l.empty())
49  {
50  GtkFileFilter* f = l.front();
51  g_object_ref_sink(f);
52  g_object_unref(f);
53  l.pop_front();
54  }
55 }

◆ filterInfoFromPath()

GtkFileFilterInfoPtr filterInfoFromPath ( const std::string &  filename)
110 {
111  GtkFileFilterInfoPtr filterInfo;
112 
113  std::unique_ptr<GFile, GObjectUnref<GFile>> const file(g_file_new_for_path(filename.c_str()));
114  std::unique_ptr<GFileInfo, GObjectUnref<GFileInfo>> const fileInfo(
115  g_file_query_info(file.get(), "standard::*", G_FILE_QUERY_INFO_NONE, nullptr, nullptr));
116  if(fileInfo)
117  {
118  // g_file_info_get_name(fileInfo) doesn't provide path info.
119  // charpFromString might throw.
120  std::unique_ptr<gchar> filenameCopy(charpFromString(filename));
121  std::unique_ptr<gchar> mime_type(charpFromString(g_content_type_get_mime_type(g_file_info_get_content_type(fileInfo.get()))));
122  std::unique_ptr<gchar> display_name(charpFromString(g_file_info_get_display_name(fileInfo.get())));
123 
124  filterInfo.reset(new GtkFileFilterInfo()); // filterInfo->filename uninitialized, so a delete is dangerous
125 
126  filterInfo->filename = filenameCopy.release();
127  filterInfo->mime_type = mime_type.release();
128  filterInfo->display_name = display_name.release();
129  filterInfo->contains =
130  static_cast<GtkFileFilterFlags>(GTK_FILE_FILTER_FILENAME | GTK_FILE_FILTER_DISPLAY_NAME | GTK_FILE_FILTER_MIME_TYPE);
131  }
132  else
133  {
134  throw std::invalid_argument("No fileinfo for file " + filename);
135  }
136 
137  return filterInfo;
138 }

Referenced by load(), and loadPresentation().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ filterMatchesInfo()

bool filterMatchesInfo ( GtkFileFilterInfo const &  info,
std::list< GtkFileFilter * > const &  filters 
)
141 {
142  for(auto const& f: filters)
143  {
144  if(gtk_file_filter_filter(f, &info))
145  {
146  return true;
147  }
148  }
149  return false;
150 }

Referenced by load(), and loadPresentation().

Here is the caller graph for this function:

◆ load() [1/3]

void load ( const std::string &  filename)
275 { load(filterInfoFromPath(filename)); }
Here is the call graph for this function:

◆ load() [2/3]

void load ( GtkFileFilterInfo const &  info)
239 {
240  const auto pm = PluginManager::getInstance();
241  const auto& openPresentationInterfaces = pm->getOpenPresentationInterfaces();
242  const auto& openInterfaces = pm->getOpenInterfaces();
243 
244  for(auto const& cur: openPresentationInterfaces)
245  {
246  std::list<GtkFileFilter*> filters = cur.first->getFilters();
247  GtkFileFilterListDestroyer const destroyer(filters);
248  if(filterMatchesInfo(info, filters))
249  {
250  PresentationInterface::Ptr const presentation = cur.first->open(info.filename);
251  if(presentation)
252  {
253  on_presentation_created(presentation);
254  find_or_create_scroom(presentation);
255  return;
256  }
257  }
258  }
259  for(auto const& cur: openInterfaces)
260  {
261  std::list<GtkFileFilter*> filters = cur.first->getFilters();
262  GtkFileFilterListDestroyer const destroyer(filters);
263  if(filterMatchesInfo(info, filters))
264  {
265  cur.first->open(info.filename, ScroomInterfaceImpl::instance());
266  return;
267  }
268  }
269 
270  throw std::invalid_argument("Don't know how to open file " + std::string(info.filename));
271 }

Referenced by load(), on_done_loading_plugins(), on_open_activate(), onDragDataReceived(), Tiff::open(), and anonymous_namespace{tiledbitmappresentation.cc}::OpenTiledBitmapAsPresentation::open().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ load() [3/3]

void load ( GtkFileFilterInfoPtr const &  info)
273 { load(*info); }
Here is the call graph for this function:

◆ loadPresentation() [1/3]

PresentationInterface::Ptr loadPresentation ( const std::string &  filename)
190 {
191  return loadPresentation(*filterInfoFromPath(filename));
192 }

Referenced by loadPresentation(), ScroomInterfaceImpl::loadPresentation(), and on_done_loading_plugins().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ loadPresentation() [2/3]

PresentationInterface::Ptr loadPresentation ( GtkFileFilterInfo const &  info)
195 {
196  static std::map<boost::filesystem::path, PresentationInterface::WeakPtr> loadedPresentations;
197  // Create canonical path from filename
198  const std::map<OpenPresentationInterface::Ptr, std::string>& openPresentationInterfaces =
199  PluginManager::getInstance()->getOpenPresentationInterfaces();
200 
201 #ifdef HAVE_BOOST_FILESYSTEM_CANONICAL
202  boost::filesystem::path key(canonical(boost::filesystem::path(info.filename)));
203 #else
204  boost::filesystem::path const key(absolute(boost::filesystem::path(info.filename)));
205 #endif
206 
207  PresentationInterface::Ptr presentation = loadedPresentations[key].lock();
208 
209  if(!presentation)
210  {
211  for(auto const& cur: openPresentationInterfaces)
212  {
213  std::list<GtkFileFilter*> const filters = cur.first->getFilters();
214  if(filterMatchesInfo(info, filters))
215  {
216  presentation = cur.first->open(info.filename);
217 
218  if(presentation)
219  {
220  loadedPresentations[key] = presentation;
221  on_presentation_created(presentation);
222  break;
223  }
224  }
225  }
226  }
227 
228  if(presentation)
229  {
230  return presentation;
231  }
232 
233  throw std::invalid_argument("Don't know how to load presentation " + std::string(info.filename));
234 }
Here is the call graph for this function:

◆ loadPresentation() [3/3]

PresentationInterface::Ptr loadPresentation ( GtkFileFilterInfoPtr const &  info)
236 { return loadPresentation(*info); }
Here is the call graph for this function:
charpFromString
char * charpFromString(const std::string &s)
Definition: loader.cc:91
find_or_create_scroom
void find_or_create_scroom(const PresentationInterface::Ptr &presentation)
Definition: callbacks.cc:481
load
void load(GtkFileFilterInfo const &info)
Definition: loader.cc:238
GtkFileFilterInfoPtr
std::unique_ptr< GtkFileFilterInfo, GtkFileFilterInfoDeleter > GtkFileFilterInfoPtr
Definition: loader.cc:44
loadPresentation
PresentationInterface::Ptr loadPresentation(const std::string &filename)
Definition: loader.cc:189
ScroomInterfaceImpl::instance
static Ptr instance()
Definition: loader.cc:279
GtkFileFilterListDestroyer
Definition: loader.cc:57
on_presentation_created
void on_presentation_created(const PresentationInterface::Ptr &presentation)
Definition: callbacks.cc:623
PresentationInterface::Ptr
boost::shared_ptr< PresentationInterface > Ptr
Definition: presentationinterface.hh:74
NewPresentationInterface::createNew
virtual PresentationInterface::Ptr createNew()=0
filterInfoFromPath
GtkFileFilterInfoPtr filterInfoFromPath(const std::string &filename)
Definition: loader.cc:109
filterMatchesInfo
bool filterMatchesInfo(GtkFileFilterInfo const &info, std::list< GtkFileFilter * > const &filters)
Definition: loader.cc:140
PluginManager::getInstance
static PluginManager::Ptr getInstance()
Definition: pluginmanager.cc:330