Back to index

unity  6.0.0
Public Member Functions | Static Public Member Functions | Public Attributes
unity::dash::LensDirectoryReader::Impl Class Reference
Collaboration diagram for unity::dash::LensDirectoryReader::Impl:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Impl (LensDirectoryReader *owner, std::string const &directory)
 ~Impl ()
void EnumerateLensesDirectoryChildren (GFileEnumerator *enumerator)
void LoadLensFile (std::string const &lensfile_path)
void GetLensDataFromKeyFile (GFile *path, const char *data, gsize length)
DataList GetLensData () const
void SortLensList ()

Static Public Member Functions

typedef std::map< GFile
*, glib::Object< GCancellable > > 
CancellableMap
static void OnDirectoryEnumerated (GFile *source, GAsyncResult *res, Impl *self)
static void LoadFileContentCallback (GObject *source, GAsyncResult *res, gpointer user_data)

Public Attributes

LensDirectoryReaderowner_
glib::Object< GFile > directory_
DataList lenses_data_
std::size_t children_waiting_to_load_
bool enumeration_done_
CancellableMap cancel_map_

Detailed Description

Definition at line 102 of file FilesystemLenses.cpp.


Member Typedef Documentation

typedef std::map<GFile*, glib::Object<GCancellable> > unity::dash::LensDirectoryReader::Impl::CancellableMap [static]

Definition at line 105 of file FilesystemLenses.cpp.


Constructor & Destructor Documentation

unity::dash::LensDirectoryReader::Impl::Impl ( LensDirectoryReader owner,
std::string const &  directory 
) [inline]

Definition at line 107 of file FilesystemLenses.cpp.

    : owner_(owner)
    , directory_(g_file_new_for_path(directory.c_str()))
    , children_waiting_to_load_(0)
    , enumeration_done_(false)
  {
    LOG_DEBUG(logger) << "Initialising lens reader for: " << directory;

    glib::Object<GCancellable> cancellable(g_cancellable_new());
    g_file_enumerate_children_async(directory_,
                                    G_FILE_ATTRIBUTE_STANDARD_NAME,
                                    G_FILE_QUERY_INFO_NONE,
                                    G_PRIORITY_DEFAULT,
                                    cancellable,
                                    (GAsyncReadyCallback)OnDirectoryEnumerated,
                                    this);
    cancel_map_[directory_] = cancellable;
  }

Definition at line 126 of file FilesystemLenses.cpp.

  {
    for (auto pair: cancel_map_)
    {
      g_cancellable_cancel(pair.second);
    }
  }

Member Function Documentation

Definition at line 168 of file FilesystemLenses.cpp.

{
  glib::Object<GCancellable> cancellable(g_cancellable_new());

  cancel_map_[g_file_enumerator_get_container(in_enumerator)] = cancellable;
  g_file_enumerator_next_files_async (in_enumerator, 64, G_PRIORITY_DEFAULT,
                                      cancellable,
                                      [] (GObject *src, GAsyncResult *res,
                                          gpointer data) -> void {
    // async callback
    glib::Error error;
    GFileEnumerator *enumerator = G_FILE_ENUMERATOR (src);
    // FIXME: won't this kill the enumerator?
    GList *files = g_file_enumerator_next_files_finish (enumerator, res, error.AsOutParam());
    if (!error)
    {
      Impl *self = (Impl*) data;
      self->cancel_map_.erase(g_file_enumerator_get_container(enumerator));
      for (GList *iter = files; iter; iter = iter->next)
      {
        glib::Object<GFileInfo> info((GFileInfo*) iter->data);

        std::string name(g_file_info_get_name(info));
        glib::String dir_path(g_file_get_path(g_file_enumerator_get_container(enumerator)));
        std::string lensfile_name = name + ".lens";

        glib::String lensfile_path(g_build_filename(dir_path.Value(),
                                                    name.c_str(),
                                                    lensfile_name.c_str(),
                                                    NULL));
        self->LoadLensFile(lensfile_path.Str());
      }
      // the GFileInfos got already freed during the iteration
      g_list_free (files);
      self->enumeration_done_ = true;
    }
    else
    {
      LOG_WARN(logger) << "Cannot enumerate over directory: " << error;
    }
  }, this);
}

Here is the call graph for this function:

Definition at line 299 of file FilesystemLenses.cpp.

{
  return lenses_data_;
}

Here is the caller graph for this function:

void unity::dash::LensDirectoryReader::Impl::GetLensDataFromKeyFile ( GFile *  path,
const char *  data,
gsize  length 
)

Definition at line 265 of file FilesystemLenses.cpp.

{
  GKeyFile* key_file = g_key_file_new();
  glib::String path(g_file_get_path(file));
  glib::Error error;

  if (g_key_file_load_from_data(key_file, data, length, G_KEY_FILE_NONE, error.AsOutParam()))
  {
    if (LensFileData::IsValid(key_file, error))
    {
      glib::String id(g_path_get_basename(path.Value()));

      lenses_data_.push_back(LensFileDataPtr(new LensFileData(key_file, id)));

      LOG_DEBUG(logger) << "Sucessfully loaded lens file " << path;
    }
    else
    {
      LOG_WARN(logger) << "Lens file  "
                       << path << " is not valid: "
                       << error;
    }
  }
  else
  {
    LOG_WARN(logger) << "Unable to load Lens file "
                     << path << ": "
                     << error;
  }
  g_key_file_free(key_file);
}

Here is the call graph for this function:

void unity::dash::LensDirectoryReader::Impl::LoadFileContentCallback ( GObject *  source,
GAsyncResult *  res,
gpointer  user_data 
) [static]

Definition at line 226 of file FilesystemLenses.cpp.

{
  Impl* self = static_cast<Impl*>(user_data);
  glib::Error error;
  glib::String contents;
  gsize length = 0;
  GFile* file = G_FILE(source);
  glib::String path(g_file_get_path(file));

  gboolean result = g_file_load_contents_finish(file, res,
                                                &contents, &length,
                                                NULL, error.AsOutParam());
  if (result)
  {
    self->GetLensDataFromKeyFile(file, contents.Value(), length);
    self->SortLensList();
  }
  else
  {
    LOG_WARN(logger) << "Unable to read lens file "
                     << path.Str() << ": "
                     << error;
    if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
      return; // self is invalid now
  }

  self->cancel_map_.erase(file);

  // If we're not waiting for any more children to load, signal that we're
  // done reading the directory
  self->children_waiting_to_load_--;
  if (self->children_waiting_to_load_ == 0)
  {
    self->owner_->load_finished.emit();
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void unity::dash::LensDirectoryReader::Impl::LoadLensFile ( std::string const &  lensfile_path)

Definition at line 211 of file FilesystemLenses.cpp.

{
  glib::Object<GFile> file(g_file_new_for_path(lensfile_path.c_str()));
  glib::Object<GCancellable> cancellable(g_cancellable_new());

  // How many files are we waiting for to load
  children_waiting_to_load_++;

  g_file_load_contents_async(file,
                             cancellable,
                             (GAsyncReadyCallback)(LensDirectoryReader::Impl::LoadFileContentCallback),
                             this);
  cancel_map_[file] = cancellable;
}

Here is the call graph for this function:

void unity::dash::LensDirectoryReader::Impl::OnDirectoryEnumerated ( GFile *  source,
GAsyncResult *  res,
Impl self 
) [static]

Definition at line 151 of file FilesystemLenses.cpp.

{
  glib::Error error;
  glib::Object<GFileEnumerator> enumerator(g_file_enumerate_children_finish(source, res, error.AsOutParam()));

  if (error || !enumerator)
  {
    glib::String path(g_file_get_path(source));
    LOG_WARN(logger) << "Unable to enumerate children of directory "
                     << path << " "
                     << error;
    return;
  }
  self->cancel_map_.erase(source);
  self->EnumerateLensesDirectoryChildren(enumerator);
}

Here is the call graph for this function:

Definition at line 304 of file FilesystemLenses.cpp.

{
  //FIXME: We don't have a strict order, but alphabetical serves us well.
  // When we have an order/policy, please replace this.
  auto sort_cb = [] (LensFileDataPtr a, LensFileDataPtr b) -> bool
  {
    if (a->id.Str() == "applications.lens")
      return true;
    else if (b->id.Str() == "applications.lens")
      return false;
    else
      return g_strcmp0(a->id.Value(), b->id.Value()) < 0;
  };
  std::sort(lenses_data_.begin(),
            lenses_data_.end(),
            sort_cb);
}

Member Data Documentation

Definition at line 148 of file FilesystemLenses.cpp.

Definition at line 146 of file FilesystemLenses.cpp.

Definition at line 144 of file FilesystemLenses.cpp.

Definition at line 147 of file FilesystemLenses.cpp.

Definition at line 145 of file FilesystemLenses.cpp.

Definition at line 143 of file FilesystemLenses.cpp.


The documentation for this class was generated from the following file: