Exemple #1
0
bool KNetAttach::validateCurrentPage()
{
    if (currentPage() == _folderType){
	_host->setFocus();
	_connectionName->setFocus();

	if (_webfolder->isChecked()) {
	    setInformationText("WebFolder");
	    updateForProtocol("WebFolder");
	    _port->setValue(80);
	} else if (_fish->isChecked()) {
	    setInformationText("Fish");
	    updateForProtocol("Fish");
	    _port->setValue(22);
	} else if (_ftp->isChecked()) {
	    setInformationText("FTP");
	    updateForProtocol("FTP");
	    _port->setValue(21);
	    if (_path->text().isEmpty()) {
		_path->setText("/");
	    }
	} else if (_smb->isChecked()) {
	    setInformationText("SMB");
	    updateForProtocol("SMB");
	} else { //if (_recent->isChecked()) {
	    KConfig recent( "krecentconnections", KConfig::NoGlobals );
	    if (!recent.hasGroup(_recentConnectionName->currentText())) {
		KConfigGroup group = recent.group("General");
		QStringList idx = group.readEntry("Index",QStringList());
		if (idx.isEmpty()) {
		    _recent->setEnabled(false);
		    if (_recent->isChecked()) {
			_webfolder->setChecked(true);
		    }
		} else {
		    _recent->setEnabled(true);
		    _recentConnectionName->addItems(idx);
		}
		return false;
	    }
	    KConfigGroup group = recent.group(_recentConnectionName->currentText());
	    _type = group.readEntry("Type");
	    setInformationText(_type);
	    if (!updateForProtocol(_type)) {
		// FIXME: handle error
	    }
	    KUrl u(group.readEntry("URL"));
	    _host->setText(u.host());
	    _user->setText(u.user());
	    _path->setText(u.path());
	    if (group.hasKey("Port")) {
		_port->setValue(group.readEntry("Port",0));
	    } else {
		_port->setValue(u.port());
	    }
	    _connectionName->setText(_recentConnectionName->currentText());
	    _createIcon->setChecked(false);
	}
	updateParametersPageStatus();

    }else{
      button(BackButton)->setEnabled(false);
      button(FinishButton)->setEnabled(false);
      KUrl url;
      if (_type == "WebFolder") {
	  if (_useEncryption->isChecked()) {
	      url.setProtocol("webdavs");
	  } else {
	      url.setProtocol("webdav");
	  }
	  url.setPort(_port->value());
      } else if (_type == "Fish") {
      KConfig config("kio_fishrc");
      KConfigGroup cg(&config, _host->text().trimmed());
      cg.writeEntry("Charset", KCharsets::charsets()->encodingForName(_encoding->currentText()));
	  url.setProtocol(_protocolText->currentText());
	  url.setPort(_port->value());
      } else if (_type == "FTP") {
	  url.setProtocol("ftp");
	  url.setPort(_port->value());
      KConfig config("kio_ftprc");
      KConfigGroup cg(&config, _host->text().trimmed());
      cg.writeEntry("Charset", KCharsets::charsets()->encodingForName(_encoding->currentText()));
      config.sync();
      } else if (_type == "SMB") {
	  url.setProtocol("smb");
      } else { // recent
      }

      url.setHost(_host->text().trimmed());
      url.setUser(_user->text().trimmed());
      QString path = _path->text().trimmed();
  #ifndef Q_WS_WIN
      // could a relative path really be made absolute by simply prepending a '/' ?
      if (!path.startsWith('/')) {
	  path = QString("/") + path;
      }
  #endif
      url.setPath(path);
    _folderParameters->setEnabled(false);
      bool success = doConnectionTest(url);
    _folderParameters->setEnabled(true);
      if (!success) {
	  KMessageBox::sorry(this, i18n("Unable to connect to server.  Please check your settings and try again."));
	  button(BackButton)->setEnabled(true);
	  return false;
      }

      KRun::runUrl(url, "inode/directory", this);

      QString name = _connectionName->text().trimmed();

      if (_createIcon->isChecked()) {
	  KGlobal::dirs()->addResourceType("remote_entries", "data", "remoteview");

	  QString path = KGlobal::dirs()->saveLocation("remote_entries");
	  path += name + ".desktop";
	  KConfig _desktopFile( path, KConfig::SimpleConfig );
	  KConfigGroup desktopFile(&_desktopFile, "Desktop Entry");
	  desktopFile.writeEntry("Icon", "folder-remote");
	  desktopFile.writeEntry("Name", name);
	  desktopFile.writeEntry("Type", "Link");
	  desktopFile.writeEntry("URL", url.prettyUrl());
      desktopFile.writeEntry("Charset", url.fileEncoding());
	  desktopFile.sync();
	  org::kde::KDirNotify::emitFilesAdded( QUrl("remote:/") );
      }

      if (!name.isEmpty()) {
	  KConfig _recent("krecentconnections", KConfig::NoGlobals);
	  KConfigGroup recent(&_recent, "General");
	  QStringList idx = recent.readEntry("Index",QStringList());
	  _recent.deleteGroup(name); // erase anything stale
	  if (idx.contains(name)) {
	      idx.removeAll(name);
	      idx.prepend(name);
	      recent.writeEntry("Index", idx);
	  } else {
	      QString last;
	      if (!idx.isEmpty()) {
		  last = idx.last();
		  idx.pop_back();
	      }
	      idx.prepend(name);
	      _recent.deleteGroup(last);
	      recent.writeEntry("Index", idx);
	  }
	recent = KConfigGroup(&_recent,name);
	  recent.writeEntry("URL", url.prettyUrl());
	  if (_type == "WebFolder" || _type == "Fish" || _type == "FTP") {
	      recent.writeEntry("Port", _port->value());
	  }
	  recent.writeEntry("Type", _type);
	  recent.sync();
      }
    }
    return true;
}