void NoteDirectoryWatcherApplicationAddin::delete_note(const Glib::ustring & note_id)
{
  DBG_OUT("NoteDirectoryWatcher: deleting %s because file deleted.", note_id.c_str());

  Glib::ustring note_uri = make_uri(note_id);

  gnote::NoteBase::Ptr note_to_delete = note_manager().find_by_uri(note_uri);
  if(note_to_delete != 0) {
    note_manager().delete_note(note_to_delete);
  }
  else {
    DBG_OUT("notedirectorywatcher: did not delete %s because note not found.", note_id.c_str());
  }
}
示例#2
0
Json::Value mesos_http::get_task_labels(const std::string& task_id)
{
	std::ostringstream os;
	CURLcode res = get_data(make_uri("/master/tasks"), os);

	Json::Value labels;
	if(res != CURLE_OK)
	{
		g_logger.log(curl_easy_strerror(res), sinsp_logger::SEV_ERROR);
		return labels;
	}

	try
	{
		Json::Value root;
		Json::Reader reader;
		if(reader.parse(os.str(), root, false))
		{
			Json::Value tasks = root["tasks"];
			if(!tasks.isNull())
			{
				for(const auto& task : tasks)
				{
					Json::Value id = task["id"];
					if(!id.isNull() && id.isString() && id.asString() == task_id)
					{
						Json::Value statuses = task["statuses"];
						if(!statuses.isNull())
						{
							double tstamp = 0.0;
							for(const auto& status : statuses)
							{
								// only task with most recent status
								// "TASK_RUNNING" considered
								Json::Value ts = status["timestamp"];
								if(!ts.isNull() && ts.isNumeric() && ts.asDouble() > tstamp)
								{
									Json::Value st = status["state"];
									if(!st.isNull() && st.isString())
									{
										if(st.asString() == "TASK_RUNNING")
										{
											labels = task["labels"];
											tstamp = ts.asDouble();
										}
										else
										{
											labels.clear();
										}
									}
								}
							}
							if(!labels.empty()) // currently running task found
							{
								return labels;
							}
						}
					}
				}
			}
		}
		else
		{
			g_logger.log("Error parsing tasks.\nJSON:\n---\n" + os.str() + "\n---", sinsp_logger::SEV_ERROR);
		}
	}
	catch(std::exception& ex)
	{
		g_logger.log(std::string("Error parsing tasks:") + ex.what(), sinsp_logger::SEV_ERROR);
	}

	return labels;
}
void NoteDirectoryWatcherApplicationAddin::add_or_update_note(const Glib::ustring & note_id)
{
  const Glib::ustring & note_path = Glib::build_filename(note_manager().notes_dir(), note_id + ".note");
  if (!sharp::file_exists(note_path)) {
    DBG_OUT("NoteDirectoryWatcher: Not processing update of %s because file does not exist.", note_path.c_str());
    return;
  }

  Glib::ustring noteXml;
  try {
    noteXml = sharp::file_read_all_text(note_path);
  }
  catch(sharp::Exception & e) {
    /* TRANSLATORS: first %s is file name, second is error */
    ERR_OUT(_("NoteDirectoryWatcher: Update aborted, error reading %s: %s"), note_path.c_str(), e.what());
    return;
  }

  if(noteXml == "") {
    DBG_OUT("NoteDirectoryWatcher: Update aborted, %s had no contents.", note_path.c_str());
    return;
  }

  Glib::ustring note_uri = make_uri(note_id);

  gnote::NoteBase::Ptr note = note_manager().find_by_uri(note_uri);

  bool is_new_note = false;

  if(note == 0) {
    is_new_note = true;
    DBG_OUT("NoteDirectoryWatcher: Adding %s because file changed.", note_id.c_str());

    Glib::ustring title;
    Glib::RefPtr<Glib::Regex> regex = Glib::Regex::create("<title>([^<]+)</title>", Glib::REGEX_MULTILINE);
    Glib::MatchInfo match_info;
    if(regex->match(noteXml, match_info)) {
      title = match_info.fetch(1);
    }
    else {
      /* TRANSLATORS: %s is file */
      ERR_OUT(_("NoteDirectoryWatcher: Error reading note title from %s"), note_path.c_str());
      return;
    }

    try {
      note = note_manager().create_with_guid(title, note_id);
      if(note == 0) {
        /* TRANSLATORS: %s is file */
        ERR_OUT(_("NoteDirectoryWatcher: Unknown error creating note from %s"), note_path.c_str());
        return;
      }
    }
    catch(std::exception & e) {
      /* TRANSLATORS: first %s is file, second is error */
      ERR_OUT(_("NoteDirectoryWatcher: Error creating note from %s: %s"), note_path.c_str(), e.what());
      return;
    }
  }

  if(is_new_note) {
    DBG_OUT("NoteDirectoryWatcher: Updating %s because file changed.", note_id.c_str());
  }
  try {
    note->load_foreign_note_xml(noteXml, gnote::CONTENT_CHANGED);
  }
  catch(std::exception & e) {
    /* TRANSLATORS: first %s is file, second is error */
    ERR_OUT(_("NoteDirectoryWatcher: Update aborted, error parsing %s: %s"), note_path.c_str(), e.what());
    if(is_new_note) {
      note_manager().delete_note(note);
    }
  }
}