<commit_before>// Copyright (c) 2014 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/content_instance.h"
#include "content/content_filter.h"

#include <media_content.h>
#include <media_filter.h>

#include <assert.h>
#include <time.h>

#include <iostream>
#include <fstream>
#include <string>
#include "common/picojson.h"

namespace {

const std::string STR_ID("id");
const std::string STR_NAME("name");
const std::string STR_DESCRIPTION("description");
const std::string STR_RATING("rating");
const std::string STR_FILTER("filter");
const std::string STR_CONTENT_URI("contentURI");
const std::string STR_EVENT_TYPE("eventType");

std::string createUriFromLocalPath(const std::string path) {
  static std::string fileScheme("file://");

  return fileScheme + path;
}

std::string getUriPath(const std::string uri) {
  static std::string fileScheme("file://");
  std::string _fileScheme = uri.substr(0, fileScheme.size());

  if (_fileScheme == fileScheme)
    return uri.substr(fileScheme.size());
  else
    return "";
}

// Tizen video meta APIs return date format "%Y:%m:%d %H:%M:%S",
// while standard JavaScript Date accept format as "%Y-%m-%d %H:%M:%S".
// So we need this conversion function.
std::string ConvertToJSDateString(const char* date) {
  struct tm result;
  if (strptime(date, "%Y:%m:%d %H:%M:%S", &result) == NULL) {
    return "";
  }

  char output_date[20];
  memset(output_date, 0, 20);
  if (!strftime(output_date, 20, "%Y-%m-%d %H:%M:%S", &result)) {
    return "";
  }

  return std::string(output_date);
}

}  // namespace

unsigned ContentInstance::m_instanceCount = 0;

ContentInstance::ContentInstance() {
  ++m_instanceCount;
  if (media_content_connect() != MEDIA_CONTENT_ERROR_NONE) {
    std::cerr << "media_content_connect: DB connection error" << std::endl;
    return;
  }
}

ContentInstance::~ContentInstance() {
  assert(m_instanceCount > 0);
  if (--m_instanceCount > 0)
    return;
  if (media_content_disconnect() != MEDIA_CONTENT_ERROR_NONE)
    std::cerr << "media_discontent_connect: error\n";
}

void ContentInstance::HandleMessage(const char* message) {
  picojson::value v;
  picojson::value::object o;

  std::string err;
  picojson::parse(v, message, message + strlen(message), &err);
  if (!err.empty()) {
    std::cerr << "Ignoring message.\n";
    return;
  }
#ifdef DEBUG_JSON_CMD
  std::cout << "HandleMessage: " << message << std::endl;
#endif
  std::string cmd = v.get("cmd").to_str();
  if (cmd == "ContentManager.getDirectories") {
    HandleGetDirectoriesRequest(v);
  } else if (cmd == "ContentManager.find") {
    HandleFindRequest(v);
  } else if (cmd == "ContentManager.scanFile") {
    HandleScanFileRequest(v);
  } else if (cmd == "ContentManager.updateBatch") {
    HandleUpdateBatchRequest(v);
  } else {
    std::cerr << "Message " + cmd + " is not supported.\n";
  }
}

void ContentInstance::PostAsyncErrorReply(const picojson::value& msg,
    WebApiAPIErrors error_code) {
  picojson::value::object o;
  o["isError"] = picojson::value(true);
  o["errorCode"] = picojson::value(static_cast<double>(error_code));
  o["replyId"] = picojson::value(msg.get("replyId").get<double>());

  picojson::value v(o);
  PostMessage(v.serialize().c_str());
}

void ContentInstance::PostAsyncSuccessReply(const picojson::value& msg,
    picojson::value::object& reply) {
  reply["isError"] = picojson::value(false);
  reply["replyId"] = picojson::value(msg.get("replyId").get<double>());
  if (msg.contains(STR_EVENT_TYPE))
    reply[STR_EVENT_TYPE] = picojson::value(msg.get(STR_EVENT_TYPE));
#ifdef DEBUG_JSON_CMD
  std::cout << "reply: " << msg.serialize().c_str() << std::endl;
#endif
  picojson::value v(reply);
  PostMessage(v.serialize().c_str());
}

void ContentInstance::PostAsyncSuccessReply(const picojson::value& msg) {
  picojson::value::object reply;
  PostAsyncSuccessReply(msg, reply);
}

void ContentInstance::PostAsyncSuccessReply(const picojson::value& msg,
    picojson::value& value) {
  picojson::value::object reply;
  reply["value"] = value;
  PostAsyncSuccessReply(msg, reply);
}

void ContentInstance::HandleSyncMessage(const char* message) {
  picojson::value v;
  picojson::value::object o;

  std::string err;
  picojson::parse(v, message, message + strlen(message), &err);
  if (!err.empty()) {
    std::cerr << "Ignoring message.\n";
    return;
  }
#ifdef DEBUG_JSON_CMD
  std::cout << "HandleSyncMessage: " << message << std::endl;
#endif
  std::string cmd = v.get("cmd").to_str();
  int rc = MEDIA_CONTENT_ERROR_INVALID_OPERATION;

  if (cmd == "ContentManager.setChangeListener") {
    rc = media_content_set_db_updated_cb(MediaContentChangeCallback, this);
  } else if (cmd == "ContentManager.unsetChangeListener") {
    rc = media_content_unset_db_updated_cb();
  } else if ("ContentManager.update") {
    if (HandleUpdateRequest(v.get("content")))
      rc = MEDIA_CONTENT_ERROR_NONE;
  } else {
    std::cerr << "Message " + cmd + " is not supported.\n";
  }

  if (rc != MEDIA_CONTENT_ERROR_NONE)
    std::cerr << "error " << cmd << std::endl;

#ifdef DEBUG_JSON_CMD
  std::cout << "Reply: " << v.serialize().c_str() << std::endl;
#endif
  SendSyncReply(v.serialize().c_str());
}

bool ContentInstance::HandleUpdateRequest(const picojson::value& msg) {
  if (!msg.contains(STR_ID)) {
    std::cerr << "HandleUpdateRequest: No ID in the message" << std::endl;
    return false;
  }

  media_info_h handle;
  if (media_info_get_media_from_db(msg.get(STR_ID).to_str().c_str(), &handle)
      != MEDIA_CONTENT_ERROR_NONE) {
    std::cerr << "media_info_get_media_from_db: error" << std::endl;
    return false;
  }

  bool no_error = true;
  if (msg.contains(STR_NAME) &&<commit_msg>[Tizen] Fix wrong condition checking<commit_after>// Copyright (c) 2014 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/content_instance.h"
#include "content/content_filter.h"

#include <media_content.h>
#include <media_filter.h>

#include <assert.h>
#include <time.h>

#include <iostream>
#include <fstream>
#include <string>
#include "common/picojson.h"

namespace {

const std::string STR_ID("id");
const std::string STR_NAME("name");
const std::string STR_DESCRIPTION("description");
const std::string STR_RATING("rating");
const std::string STR_FILTER("filter");
const std::string STR_CONTENT_URI("contentURI");
const std::string STR_EVENT_TYPE("eventType");

std::string createUriFromLocalPath(const std::string path) {
  static std::string fileScheme("file://");

  return fileScheme + path;
}

std::string getUriPath(const std::string uri) {
  static std::string fileScheme("file://");
  std::string _fileScheme = uri.substr(0, fileScheme.size());

  if (_fileScheme == fileScheme)
    return uri.substr(fileScheme.size());
  else
    return "";
}

// Tizen video meta APIs return date format "%Y:%m:%d %H:%M:%S",
// while standard JavaScript Date accept format as "%Y-%m-%d %H:%M:%S".
// So we need this conversion function.
std::string ConvertToJSDateString(const char* date) {
  struct tm result;
  if (strptime(date, "%Y:%m:%d %H:%M:%S", &result) == NULL) {
    return "";
  }

  char output_date[20];
  memset(output_date, 0, 20);
  if (!strftime(output_date, 20, "%Y-%m-%d %H:%M:%S", &result)) {
    return "";
  }

  return std::string(output_date);
}

}  // namespace

unsigned ContentInstance::m_instanceCount = 0;

ContentInstance::ContentInstance() {
  ++m_instanceCount;
  if (media_content_connect() != MEDIA_CONTENT_ERROR_NONE) {
    std::cerr << "media_content_connect: DB connection error" << std::endl;
    return;
  }
}

ContentInstance::~ContentInstance() {
  assert(m_instanceCount > 0);
  if (--m_instanceCount > 0)
    return;
  if (media_content_disconnect() != MEDIA_CONTENT_ERROR_NONE)
    std::cerr << "media_discontent_connect: error\n";
}

void ContentInstance::HandleMessage(const char* message) {
  picojson::value v;
  picojson::value::object o;

  std::string err;
  picojson::parse(v, message, message + strlen(message), &err);
  if (!err.empty()) {
    std::cerr << "Ignoring message.\n";
    return;
  }
#ifdef DEBUG_JSON_CMD
  std::cout << "HandleMessage: " << message << std::endl;
#endif
  std::string cmd = v.get("cmd").to_str();
  if (cmd == "ContentManager.getDirectories") {
    HandleGetDirectoriesRequest(v);
  } else if (cmd == "ContentManager.find") {
    HandleFindRequest(v);
  } else if (cmd == "ContentManager.scanFile") {
    HandleScanFileRequest(v);
  } else if (cmd == "ContentManager.updateBatch") {
    HandleUpdateBatchRequest(v);
  } else {
    std::cerr << "Message " + cmd + " is not supported.\n";
  }
}

void ContentInstance::PostAsyncErrorReply(const picojson::value& msg,
    WebApiAPIErrors error_code) {
  picojson::value::object o;
  o["isError"] = picojson::value(true);
  o["errorCode"] = picojson::value(static_cast<double>(error_code));
  o["replyId"] = picojson::value(msg.get("replyId").get<double>());

  picojson::value v(o);
  PostMessage(v.serialize().c_str());
}

void ContentInstance::PostAsyncSuccessReply(const picojson::value& msg,
    picojson::value::object& reply) {
  reply["isError"] = picojson::value(false);
  reply["replyId"] = picojson::value(msg.get("replyId").get<double>());
  if (msg.contains(STR_EVENT_TYPE))
    reply[STR_EVENT_TYPE] = picojson::value(msg.get(STR_EVENT_TYPE));
#ifdef DEBUG_JSON_CMD
  std::cout << "reply: " << msg.serialize().c_str() << std::endl;
#endif
  picojson::value v(reply);
  PostMessage(v.serialize().c_str());
}

void ContentInstance::PostAsyncSuccessReply(const picojson::value& msg) {
  picojson::value::object reply;
  PostAsyncSuccessReply(msg, reply);
}

void ContentInstance::PostAsyncSuccessReply(const picojson::value& msg,
    picojson::value& value) {
  picojson::value::object reply;
  reply["value"] = value;
  PostAsyncSuccessReply(msg, reply);
}

void ContentInstance::HandleSyncMessage(const char* message) {
  picojson::value v;
  picojson::value::object o;

  std::string err;
  picojson::parse(v, message, message + strlen(message), &err);
  if (!err.empty()) {
    std::cerr << "Ignoring message.\n";
    return;
  }
#ifdef DEBUG_JSON_CMD
  std::cout << "HandleSyncMessage: " << message << std::endl;
#endif
  std::string cmd = v.get("cmd").to_str();
  int rc = MEDIA_CONTENT_ERROR_INVALID_OPERATION;

  if (cmd == "ContentManager.setChangeListener") {
    rc = media_content_set_db_updated_cb(MediaContentChangeCallback, this);
  } else if (cmd == "ContentManager.unsetChangeListener") {
    rc = media_content_unset_db_updated_cb();
  } else if (cmd == "ContentManager.update") {
    if (HandleUpdateRequest(v.get("content")))
      rc = MEDIA_CONTENT_ERROR_NONE;
  } else {
    std::cerr << "Message " + cmd + " is not supported.\n";
  }

  if (rc != MEDIA_CONTENT_ERROR_NONE)
    std::cerr << "error " << cmd << std::endl;

#ifdef DEBUG_JSON_CMD
  std::cout << "Reply: " << v.serialize().c_str() << std::endl;
#endif
  SendSyncReply(v.serialize().c_str());
}

bool ContentInstance::HandleUpdateRequest(const picojson::value& msg) {
  if (!msg.contains(STR_ID)) {
    std::cerr << "HandleUpdateRequest: No ID in the message" << std::endl;
    return false;
  }

  media_info_h handle;
  if (media_info_get_media_from_db(msg.get(STR_ID).to_str().c_str(), &handle)
      != MEDIA_CONTENT_ERROR_NONE) {
    std::cerr << "media_info_get_media_from_db: error" << std::endl;
    return false;
  }

  bool no_error = true;
  if (msg.contains(STR_NAME) &&
{'fraction_non_alphanumeric': 0.16474237463381008, 'fraction_numerical': 0.0025848698948819577, 'mean_word_length': 5.1121642969984205, 'pattern_counts': {'":': 0, '<': 127, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3690414', 'n_tokens_mistral': 3997, 'n_tokens_neox': 3935, 'n_words': 815}