Пример #1
0
void ExplorerTreeWidget::ui_dropCollection()
{
    ExplorerCollectionTreeItem *collectionItem = selectedCollectionItem();
    if (!collectionItem)
        return;

    MongoCollection *collection = collectionItem->collection();
    MongoDatabase *database = collection->database();
    MongoServer *server = database->server();
    ConnectionSettings *settings = server->connectionRecord();

    // Ask user
    int answer = QMessageBox::question(this,
            "Drop Collection",
            QString("Drop <b>%1</b> collection?").arg(collection->name()),
            QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton);

    if (answer != QMessageBox::Yes)
        return;

    database->dropCollection(collection->name());
    database->loadCollections();

    //openCurrentCollectionShell("db.%1.drop()", false);
}
Пример #2
0
void ExplorerTreeWidget::ui_addDocument()
{
    ExplorerCollectionTreeItem *collectionItem = selectedCollectionItem();
    if (!collectionItem)
        return;

    MongoCollection *collection = collectionItem->collection();
    MongoDatabase *database = collection->database();
    MongoServer *server = database->server();
    ConnectionSettings *settings = server->connectionRecord();

    DocumentTextEditor editor(settings->getFullAddress(), database->name(),
                              collection->name(), "{\n    \n}");

    editor.setCursorPosition(1, 4);
    editor.setWindowTitle("Insert Document");
    int result = editor.exec();
    activateWindow();

    if (result == QDialog::Accepted) {
        mongo::BSONObj obj = editor.bsonObj();
        server->insertDocument(obj, database->name(), collection->name());
    }

    /*
    openCurrentCollectionShell(
        "db.%1.insert({\n"
        "    '' : '',\n"
        "})"
    , false, CursorPosition(1, 5));
    */
}
Пример #3
0
void ExplorerTreeWidget::ui_renameCollection()
{
    ExplorerCollectionTreeItem *collectionItem = selectedCollectionItem();
    if (!collectionItem)
        return;

    MongoCollection *collection = collectionItem->collection();
    MongoDatabase *database = collection->database();
    MongoServer *server = database->server();
    ConnectionSettings *settings = server->connectionRecord();

    CreateDatabaseDialog dlg(settings->getFullAddress(),
                             database->name(),
                             collection->name());
    dlg.setWindowTitle("Rename Collection");
    dlg.setOkButtonText("&Rename");
    dlg.setInputLabelText("New Collection Name:");
    dlg.setInputText(collection->name());
    int result = dlg.exec();

    if (result == QDialog::Accepted) {
        database->renameCollection(collection->name(), dlg.databaseName());

        // refresh list of collections
        database->loadCollections();
    }
}
Пример #4
0
void ExplorerTreeWidget::ui_editUser()
{
    ExplorerUserTreeItem *userItem = selectedUserItem();
    if (!userItem)
        return;

    MongoUser user = userItem->user();
    MongoDatabase *database = userItem->database();
    MongoServer *server = database->server();

    CreateUserDialog dlg(server->connectionRecord()->getFullAddress(),
                         database->name(),
                         user);
    dlg.setWindowTitle("Edit User");
    dlg.setUserPasswordLabelText("New Password:");
    int result = dlg.exec();

    if (result == QDialog::Accepted) {

        MongoUser user = dlg.user();
        database->createUser(user, true);

        // refresh list of users
        database->loadUsers();
    }
}
    void ExplorerServerTreeItem::databaseRefreshed(const QList<MongoDatabase *> &dbs)
    {
        int count = dbs.count();
        setText(0, buildServerName(&count));

        // Remove child items
        QtUtils::clearChildItems(this);

        // Add 'System' folder
        QIcon folderIcon = GuiRegistry::instance().folderIcon();
        ExplorerTreeItem *systemFolder = new ExplorerTreeItem(this);
        systemFolder->setIcon(0, folderIcon);
        systemFolder->setText(0, "System");
        addChild(systemFolder);

        for (int i = 0; i < dbs.size(); i++)
        {
            MongoDatabase *database = dbs.at(i);

            if (database->isSystem()) {
                ExplorerDatabaseTreeItem *dbItem = new ExplorerDatabaseTreeItem(systemFolder,database);
                systemFolder->addChild(dbItem);
                continue;
            }

            ExplorerDatabaseTreeItem *dbItem = new ExplorerDatabaseTreeItem(this,database);
            addChild(dbItem);
        }

        // Show 'System' folder only if it has items
        systemFolder->setHidden(systemFolder->childCount() == 0);
    }
Пример #6
0
void ExplorerTreeWidget::ui_removeAllDocuments()
{
    ExplorerCollectionTreeItem *collectionItem = selectedCollectionItem();
    if (!collectionItem)
        return;

    MongoCollection *collection = collectionItem->collection();
    MongoDatabase *database = collection->database();
    MongoServer *server = database->server();
    ConnectionSettings *settings = server->connectionRecord();

    // Ask user
    int answer = QMessageBox::question(this,
            "Remove All Documents",
            QString("Remove all documents from <b>%1</b> collection?").arg(collection->name()),
            QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton);

    if (answer != QMessageBox::Yes)
        return;

    mongo::BSONObjBuilder builder;
    mongo::BSONObj bsonQuery = builder.obj();
    mongo::Query query(bsonQuery);

    server->removeDocuments(query, database->name(), collection->name(), false);
}
Пример #7
0
void ExplorerTreeWidget::ui_editFunction()
{
    ExplorerFunctionTreeItem *functionItem = selectedFunctionItem();
    if (!functionItem)
        return;

    MongoFunction function= functionItem->function();
    MongoDatabase *database = functionItem->database();
    MongoServer *server = database->server();
    QString name = function.name();

    FunctionTextEditor dlg(server->connectionRecord()->getFullAddress(),
                         database->name(),
                         function);
    dlg.setWindowTitle("Edit Function");
    int result = dlg.exec();

    if (result == QDialog::Accepted) {

        MongoFunction editedFunction = dlg.function();
        database->updateFunction(name, editedFunction);

        // refresh list of functions
        database->loadFunctions();
    }
}
Пример #8
0
 QStringList MongoServer::getDatabasesNames() const {
     QStringList result;
     for (QList<MongoDatabase *>::const_iterator it = _databases.begin(); it != _databases.end(); ++it) {
         MongoDatabase *datab = *it;
         result.append(QtUtils::toQString(datab->name()));
     }
     return result;
 }
Пример #9
0
 MongoDatabase *MongoServer::findDatabaseByName(const std::string &dbName) const {
     for (QList<MongoDatabase *>::const_iterator it = _databases.begin(); it != _databases.end(); ++it) {
         MongoDatabase *datab = *it;
         if (datab->name() == dbName) {
             return datab;
         }
     }
     return NULL;
 }
Пример #10
0
    void ExplorerCollectionTreeItem::ui_dropCollection()
    {
        // Ask user
        int answer = utils::questionDialog(treeWidget(), "Drop", "collection", QtUtils::toQString(_collection->name()));

        if (answer == QMessageBox::Yes) {
            MongoDatabase *database = _collection->database();
            database->dropCollection(_collection->name());
        }
    }
void ExportMoleculeHandler::exportMolecule()
{
  if (m_molecule.isValid()) {
    // Retrieve the molecule from the database.
    MongoDatabase *db = MongoDatabase::instance();
    mongo::BSONObj moleculeObj = db->fetchMolecule(m_molecule);

    Avogadro::Core::Molecule mol;

    // Check for atoms in the molecule.
    if (moleculeObj.hasField("atoms")) {
      // Generate chemical json string.
      std::string cjson = CjsonExporter::toCjson(moleculeObj);

      // Create a molecule object for export.
      FileFormatManager::instance().readString(mol, cjson, "cjson");
    }
    else if (moleculeObj.hasField("inchi")) {
      // Attempt to load from InChI otherwise.
      FileFormatManager::instance().readString(mol,
                                               moleculeObj.getStringField("inchi"),
                                               "inchi");
    }

    // get molecule name to set default file name
    QString moleculeName = moleculeObj.getStringField("name");

    if (moleculeName.isEmpty())
      moleculeName = "molecule";

    // Pop-up dialog and get file name
    QString fileName = QFileDialog::getSaveFileName(0,
                                                    tr("Save File"),
                                                    QDir::currentPath() +
                                                    QDir::separator() +
                                                    moleculeName + ".cml");

    if (fileName.isEmpty())
      return;

    bool ok = FileFormatManager::instance().writeFile(mol,
                                                      fileName.toStdString());
    if(!ok) {
      // writing failed, display an error dialog
      QString errorMessage =
          QString("Failed to write molecule to file (%1).").arg(fileName);

      QMessageBox::critical(0, "Molecule Export Failed", errorMessage);
    }
  }
}
Пример #12
0
std::string CjsonExporter::toCjson(const mongo::BSONObj &mongoChemObj)
{
  // Follow the database link and convert to CJSON.
  MongoDatabase *db = MongoDatabase::instance();
  if (!db)
    return "";

  mongo::BSONObj structure = mongoChemObj.getObjectField("3dStructure");
  if (!structure.hasField("$ref") || !structure.hasField("$id")
      || !structure.getField("$id").isSimpleType()) {
    return "";
  }
  std::auto_ptr<mongo::DBClientCursor> cursor =
      db->query(db->databaseName() + "." + structure.getStringField("$ref"),
                QUERY("_id" << structure.getField("$id").OID()), 1);
  mongo::BSONObj object;
  if (cursor->more())
    object = cursor->next();
  else
    return "";

  std::vector<std::string> toCopy;
  toCopy.push_back("name");
  toCopy.push_back("inchi");
  toCopy.push_back("formula");
  toCopy.push_back("properties");
  mongo::BSONObjBuilder builder;

  for (size_t i = 0; i < toCopy.size(); i++) {
    mongo::BSONElement field = mongoChemObj.getField(toCopy[i]);
    if (!field.eoo())
      builder.append(field);
  }
  toCopy.clear();
  toCopy.push_back("atoms");
  toCopy.push_back("bonds");
  for (size_t i = 0; i < toCopy.size(); i++) {
    mongo::BSONElement field = object.getField(toCopy[i]);
    if (!field.eoo())
      builder.append(field);
  }

  // Add the chemical JSON version field.
  builder.append("chemical json", 0);

  mongo::BSONObj obj = builder.obj();

  return obj.jsonString(mongo::Strict);
}
    void ExplorerCollectionTreeItem::ui_removeAllDocuments()
    {
        MongoDatabase *database = _collection->database();
        // Ask user
        int answer = QMessageBox::question(treeWidget(),
            "Remove All Documents",
            QString("Remove all documents from <b>%1</b> collection?").arg(QtUtils::toQString(_collection->name())),
            QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton);

        if (answer == QMessageBox::Yes) {
            MongoServer *server = database->server();
            mongo::BSONObjBuilder builder;
            mongo::BSONObj bsonQuery = builder.obj();
            mongo::Query query(bsonQuery);
            server->removeDocuments(query, MongoNamespace(database->name(), _collection->name()), false);
        }
    }
    void ExplorerCollectionTreeItem::ui_addDocument()
    {
        MongoDatabase *database = _collection->database();
        MongoServer *server = database->server();
        ConnectionSettings *settings = server->connectionRecord();

        DocumentTextEditor editor(CollectionInfo(settings->getFullAddress(), database->name(), _collection->name()), "{\n    \n}");

        editor.setCursorPosition(1, 4);
        editor.setWindowTitle("Insert Document");
        int result = editor.exec();

        treeWidget()->activateWindow();

        if (result == QDialog::Accepted) {
            server->insertDocuments(editor.bsonObj(), MongoNamespace(database->name(), _collection->name()) );
        }
    }
void ComputationalResultsTableView::contextMenuEvent(QContextMenuEvent *e)
{
  MongoDatabase *db = MongoDatabase::instance();
  QModelIndex index = indexAt(e->pos());

  if (index.isValid()) {
    mongo::BSONObj *obj =
      static_cast<mongo::BSONObj *>(index.internalPointer());

    QMenu *menu = new QMenu(this);

    QAction *action;

    // add view and save log file actions
    if (obj && obj->hasField("log_file")) {
      action = menu->addAction("&View Log File",
                               this,
                               SLOT(showLogFile()));

      action = menu->addAction("&Save Log File",
                               this,
                               SLOT(showLogFile()));
    }

    // add open in editor action
    action = menu->addAction("&Open in Editor");
    MoleculeRef ref = db->findMoleculeFromBSONObj(obj);
    m_openInEditorHandler->setMolecule(ref);
    connect(action, SIGNAL(triggered()),
            m_openInEditorHandler, SLOT(openInEditor()));

    // add details action
    if (m_enableShowDetailsAction) {
      action = menu->addAction("Show Molecule &Details",
                               this,
                               SLOT(showMoleculeDetailsDialog()));
    }

    m_row = index.row();

    menu->exec(QCursor::pos());
  }
}
void ComputationalResultsTableView::showMoleculeDetailsDialog()
{
  MongoDatabase *db = MongoDatabase::instance();
  mongo::BSONObj *obj =
    static_cast<mongo::BSONObj *>(currentIndex().internalPointer());
  std::string inchikey = obj->getStringField("inchikey");
  MoleculeRef ref = db->findMoleculeFromInChIKey(inchikey);

  if (ref.isValid()) {
    MoleculeDetailDialog *dialog = new MoleculeDetailDialog(this);
    dialog->setMolecule(ref);
    dialog->show();
  }
  else {
    QMessageBox::critical(this,
                          "Error",
                          "Failed to find molecule in collection.");
  }
}
Пример #17
0
    void ExplorerCollectionTreeItem::ui_renameCollection()
    {
        MongoDatabase *database = _collection->database();
        MongoServer *server = database->server();
        ConnectionSettings *connSettings = server->connectionRecord();

        CreateDatabaseDialog dlg(QtUtils::toQString(connSettings->getFullAddress()),
            QtUtils::toQString(database->name()),
            QtUtils::toQString(_collection->name()), treeWidget());
        dlg.setWindowTitle("Rename Collection");
        dlg.setOkButtonText("&Rename");
        dlg.setInputLabelText("New Collection Name:");
        dlg.setInputText(QtUtils::toQString(_collection->name()));
        int result = dlg.exec();

        if (result == QDialog::Accepted) {
            database->renameCollection(_collection->name(), QtUtils::toStdString(dlg.databaseName()));
        }
    }
Пример #18
0
void ExplorerTreeWidget::ui_dropFunction()
{
    ExplorerFunctionTreeItem *functionItem = selectedFunctionItem();
    if (!functionItem)
        return;

    MongoFunction function = functionItem->function();
    MongoDatabase *database = functionItem->database();

    // Ask user
    int answer = QMessageBox::question(this,
            "Remove Function",
            QString("Remove <b>%1</b> function?").arg(function.name()),
            QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton);

    if (answer != QMessageBox::Yes)
        return;

    database->dropFunction(function.name());
    database->loadFunctions(); // refresh list of functions
}
Пример #19
0
void ExplorerTreeWidget::ui_dropUser()
{
    ExplorerUserTreeItem *userItem = selectedUserItem();
    if (!userItem)
        return;

    MongoUser user = userItem->user();
    MongoDatabase *database = userItem->database();
    MongoServer *server = database->server();

    // Ask user
    int answer = QMessageBox::question(this,
            "Remove User",
            QString("Remove <b>%1</b> user?").arg(user.name()),
            QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton);

    if (answer != QMessageBox::Yes)
        return;

    database->dropUser(user.id());
    database->loadUsers(); // refresh list of users
}
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.");
  }
}
Пример #21
0
void ExplorerTreeWidget::ui_dbDrop()
{
    ExplorerDatabaseTreeItem *dbItem = selectedDatabaseItem();
    if (!dbItem)
        return;

    MongoDatabase *database = dbItem->database();
    MongoServer *server = database->server();

    // Ask user
    int answer = QMessageBox::question(this,
            "Drop Database",
            QString("Drop <b>%1</b> database?").arg(database->name()),
            QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton);

    if (answer != QMessageBox::Yes)
        return;

    server->dropDatabase(database->name());
    server->loadDatabases(); // refresh list of databases

    //openCurrentDatabaseShell("db.dropDatabase()", false);
}
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();
}
    void ExplorerCollectionTreeItem::ui_copyToCollectionToDiffrentServer()
    {
        MongoDatabase *databaseFrom = _collection->database();
        MongoServer *server = databaseFrom->server();
        ConnectionSettings *settings = server->connectionRecord();

        CopyCollection dlg(QtUtils::toQString(settings->getFullAddress()), QtUtils::toQString(databaseFrom->name()), QtUtils::toQString(_collection->name()) );
        int result = dlg.exec();

        if (result == QDialog::Accepted) {
            MongoDatabase *databaseTo = dlg.selectedDatabase();
            databaseTo->copyCollection(server, databaseFrom->name(), _collection->name());
            databaseTo->loadCollections();
        }
    }