void ComputationalResultsTableView::showLogFile()
{
  MongoDatabase *db = MongoDatabase::instance();
  mongo::GridFS fs(*db->connection(), "chem", "fs");

  mongo::BSONObj *obj =
    static_cast<mongo::BSONObj *>(currentIndex().internalPointer());
  if (obj) {
    const char *file_name = obj->getStringField("log_file");

    mongo::GridFile file = fs.findFile(file_name);

    if (!file.exists()) {
      QMessageBox::critical(this,
                            "Error",
                            "Failed to load output file.");
      return;
    }

    // load file into a buffer
    std::stringstream stream;
    file.write(stream);

    QTextEdit *viewer = new QTextEdit(this);
    viewer->resize(500, 600);
    viewer->setWindowFlags(Qt::Dialog);
    viewer->setWindowTitle(file_name);
    viewer->setReadOnly(true);
    std::string file_data_string = stream.str();
    viewer->setText(file_data_string.c_str());
    viewer->show();
  }
  else {
    QMessageBox::critical(this,
                          "Error",
                          "Failed to load output file.");
  }
}
void ComputationalResultsModel::setQuery(const mongo::Query &query)
{
  beginInsertRows(QModelIndex(), 0, rowCount(QModelIndex()));

  m_objects.clear();

  MongoDatabase *db = MongoDatabase::instance();

  try {
    std::string collection = db->databaseName();
    std::auto_ptr<mongo::DBClientCursor> cursor =
        db->connection()->query(collection + ".quantum", query);

    // Let's read in the results, but limit to 1000 in case of bad queries.
    int i(0);
    while (cursor->more() && ++i < 1000)
      m_objects.push_back(cursor->next().copy());
  }
  catch (mongo::SocketException &e) {
    std::cerr << "Failed to query MongoDB: " << e.what() << std::endl;
  }

  endInsertRows();
}