int ModelManager::snapshotsFromFile(const std::string& path_in, bool add){ std::string path = path_in; if(path.empty()){ if(!defaultFilePath(path)) return 0; } int r=0; FILE * fp = fopen(path.c_str(), "r"); if(fp){ char buf[512]; std::string s; while(!feof(fp)){ int n = fread(buf, sizeof(buf[0]), sizeof(buf), fp); s.append(buf, n); r += n; } if(!add) clearSnapshots(); snapshotsFromString(s); fclose(fp); } return r; }
void UIWizardNewVDPage3::onSelectLocationButtonClicked() { /* Get current folder and filename: */ QFileInfo fullFilePath(mediumPath()); QDir folder = fullFilePath.path(); QString strFileName = fullFilePath.fileName(); /* Set the first parent folder that exists as the current: */ while (!folder.exists() && !folder.isRoot()) { QFileInfo folderInfo(folder.absolutePath()); if (folder == QDir(folderInfo.absolutePath())) break; folder = folderInfo.absolutePath(); } /* But if it doesn't exists at all: */ if (!folder.exists() || folder.isRoot()) { /* Use recommended one folder: */ QFileInfo defaultFilePath(absoluteFilePath(strFileName, m_strDefaultPath)); folder = defaultFilePath.path(); } /* Prepare backends list: */ QVector<QString> fileExtensions; QVector<KDeviceType> deviceTypes; CMediumFormat mediumFormat = fieldImp("mediumFormat").value<CMediumFormat>(); mediumFormat.DescribeFileExtensions(fileExtensions, deviceTypes); QStringList validExtensionList; for (int i = 0; i < fileExtensions.size(); ++i) if (deviceTypes[i] == KDeviceType_HardDisk) validExtensionList << QString("*.%1").arg(fileExtensions[i]); /* Compose full filter list: */ QString strBackendsList = QString("%1 (%2)").arg(mediumFormat.GetName()).arg(validExtensionList.join(" ")); /* Open corresponding file-dialog: */ QString strChosenFilePath = QIFileDialog::getSaveFileName(folder.absoluteFilePath(strFileName), strBackendsList, thisImp(), VBoxGlobal::tr("Please choose a location for new virtual hard drive file")); /* If there was something really chosen: */ if (!strChosenFilePath.isEmpty()) { /* If valid file extension is missed, append it: */ if (QFileInfo(strChosenFilePath).suffix().isEmpty()) strChosenFilePath += QString(".%1").arg(m_strDefaultExtension); m_pLocationEditor->setText(QDir::toNativeSeparators(strChosenFilePath)); m_pLocationEditor->selectAll(); m_pLocationEditor->setFocus(); } }
int ModelManager::snapshotsToFile(const std::string& path_in) const { std::string s; if(!snapshotsToString(s)) return 0; std::string path = path_in; if(path.empty()){ if(!defaultFilePath(path)) return 0; } int r=0; FILE * fp = fopen(path.c_str(), "w"); if(fp){ r = fwrite(s.c_str(), sizeof(std::string::value_type), s.size(), fp); fclose(fp); } return r; }
void NewProjectPage::setDefaults() { if(mFrontloader != NULL) { buildTemplateList(); QString defaultPath = getValidProjectName("New Project"); QFileInfo defaultFilePath(defaultPath); // set the default project name NameTextEdit->setText(defaultFilePath.fileName()); // set the default location DirectoryTextEdit->setText(defaultFilePath.absolutePath()); // Default module list instance mCurrentInstance = new ModuleListInstance(); mCurrentInstance->buildInstances(mFrontloader->getModuleList()); } }
void WebPage::handleBase64Download(QWebFrame* pWebFrame, QUrl url) { // look for beginning of base64 data QString base64 = QString::fromUtf8("base64,"); int pos = url.path().indexOf(base64); if (pos == -1) { LOG_ERROR_MESSAGE("Base64 designator not found in data url"); return; } // extract the base64 data from the uri QString base64Data = url.path(); base64Data.remove(0, pos + base64.length()); QByteArray base64ByteArray(base64Data.toStdString().c_str()); QByteArray byteArray = QByteArray::fromBase64(base64ByteArray); // find the a tag in the page with this href QWebElement aTag; QString urlString = url.toString(QUrl::None); QWebElementCollection aElements = pWebFrame->findAllElements( QString::fromUtf8("a")); for (int i=0; i<aElements.count(); i++) { QWebElement a = aElements.at(i); QString href = a.attribute(QString::fromUtf8("href")); href.replace(QChar::fromAscii('\n'), QString::fromUtf8("")); if (href == urlString) { aTag = a; break; } } // if no a tag was found then bail if (aTag.isNull()) { LOG_ERROR_MESSAGE("Unable to finding matching a tag for data url"); return; } // get the download attribute (default filename) QString download = aTag.attribute(QString::fromUtf8("download")); QString defaultFilename = defaultSaveDir_.absoluteFilePath(download); // prompt for filename QString filename = QFileDialog::getSaveFileName( NULL, tr("Download File"), defaultFilename, defaultSaveDir_.absolutePath(), 0, standardFileDialogOptions()); if (!filename.isEmpty()) { // see if we need to force the extension FilePath defaultFilePath(defaultFilename.toUtf8().constData()); FilePath chosenFilePath(filename.toUtf8().constData()); if (chosenFilePath.extension().empty() && !defaultFilePath.extension().empty()) { filename += QString::fromStdString(defaultFilePath.extension()); } // write the file QFile file(filename); if (file.open(QIODevice::WriteOnly)) { if (file.write(byteArray) == -1) { showFileError(QString::fromUtf8("writing"), filename, file.errorString()); } file.close(); } else { showFileError(QString::fromUtf8("writing"), filename, file.errorString()); } // persist the defaultSaveDir defaultSaveDir_ = QFileInfo(file).dir(); } }