Esempio n. 1
0
void ClientServer::openFile()
{

  QString filter(QString("%1 (*.cml);;%2 (*.cjson)")
                  .arg(tr("Chemical Markup Language"))
                  .arg(tr("Chemical JSON")));

  QSettings settings;
  QString dir = settings.value("MainWindow/lastOpenDir").toString();

  QString fileName
    = QFileDialog::getOpenFileName(qobject_cast<QWidget*>(parent()),
                                   tr("Open remote chemical file"),
                                   dir, filter);

  if (fileName.isEmpty()) // user cancel
    return;

  QFileInfo info(fileName);
  dir = info.absoluteDir().absolutePath();
  settings.setValue("MainWindow/lastOpenDir", dir);

  if (!isConnected()) {
    QString host = settings.value("ConnectionSettings/hostName").toString();
    int port = settings.value("ConnectionSettings/port").toInt();

    if (!connectToServer(host.toLocal8Bit().data(), port)) {
      QMessageBox::critical(qobject_cast<QWidget*>(this->parent()),
                            tr("Connection failed"),
                            tr("The connection to %2:%3 failed: connection"
                               " refused.").arg(host).arg(port));
      return;
    }
  }

  RemoteMoleculeService::Proxy proxy(m_channel);

  OpenRequest request;
  if (fileName.toLower().endsWith("cjson"))
    request.set_format("cjson");
  else
    request.set_format("cml");

  request.set_path(fileName.toLocal8Bit().data());

  OpenResponse *response = new OpenResponse();
  Closure *callback = NewCallback(this, &ClientServer::handleResponse,
     response);

 proxy.open(&request, response, callback);

}
    OpenResponse OpenFile(const string &path, int flags) {
        OpenRequest request;
        request.set_path(path);
        request.set_flags(flags);

        OpenResponse response;
        ClientContext context;
        Status status = stub_->OpenFile(&context, request, &response);
        if (status.ok()) {
            return response;
        }
        // TODO: Do something on failure here
        return response;
    }