void QxFileBrowser::updateGotoMenu() { for (int i = 0, n = cwdPast_.count(); i < n; ++i) { if (!QDir(cwdPast_.at(i)).exists()) { cwdPast_.removeAt(i); --i; --n; } } if (gotoMenu_) { disconnect(gotoMenu_, 0, 0, 0); gotoMenu_->setAttribute(Qt::WA_DeleteOnClose, true); // destruction workaround, HACK gotoMenu_->close(); } gotoMenu_ = new QMenu(this); { QFont font = gotoMenu_->font(); font.setPixelSize(styleManager()->constant("dirOpenMenuFontSizePx")); gotoMenu_->setFont(font); } connect(gotoMenu_, SIGNAL(triggered(QAction*)), this, SLOT(cwdSet(QAction*))); actionToPath_.clear(); QFileIconProvider iconProvider; QDir dir = QDir::current(); while (!dir.isRoot()) { dir.cdUp(); QAction* action = gotoMenu_->addAction(iconProvider.icon(QFileInfo(dir.path())), dir.isRoot() ? QDir::separator() : dir.dirName()); actionToPath_[action] = dir.path(); } gotoMenu_->addSeparator(); recentMenu_ = gotoMenu_->addMenu(tr("Recent Places")); { QFont font = recentMenu_->font(); font.setPixelSize(styleManager()->constant("dirOpenMenuFontSizePx")); recentMenu_->setFont(font); } // QxDesignHack::beautify(recentMenu_); connect(recentMenu_, SIGNAL(triggered(QAction*)), this, SLOT(cwdSet(QAction*))); int n = cwdPast_.count(); recentMenu_->setDisabled(n == 0); for (int i = n-1; i >= 0; --i) { QString path = cwdPast_[i]; QString name = path; // QFileInfo(path).fileName(); if (name == QString()) name = QDir::separator(); QAction* action = recentMenu_->addAction(iconProvider.icon(QFileInfo(path)), name); actionToPath_[action] = path; } gotoButton_->setMenu(gotoMenu_); }
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(); } }
/* static */ QString QIFileDialog::getFirstExistingDir (const QString &aStartDir) { QString result = QString::null; QDir dir (aStartDir); while (!dir.exists() && !dir.isRoot()) { QFileInfo dirInfo (dir.absolutePath()); if (dir == QDir(dirInfo.absolutePath())) break; dir = dirInfo.absolutePath(); } if (dir.exists() && !dir.isRoot()) result = dir.absolutePath(); return result; }
QString AddToArchive::detectBaseName(const QStringList &paths) const { QFileInfo fileInfo = QFileInfo(paths.first()); QDir parentDir = fileInfo.dir(); QString base = parentDir.absolutePath() + QLatin1Char('/'); if (paths.size() > 1) { if (!parentDir.isRoot()) { // Use directory name for the new archive. base += parentDir.dirName(); } } else { // Strip filename of its extension, but only if present (see #362690). if (!QMimeDatabase().mimeTypeForFile(fileInfo.fileName(), QMimeDatabase::MatchExtension).isDefault()) { base += fileInfo.completeBaseName(); } else { base += fileInfo.fileName(); } } // Special case for compressed tar archives. if (base.right(4).toUpper() == QLatin1String(".TAR")) { base.chop(4); } if (base.endsWith(QLatin1Char('/'))) { base.chop(1); } return base; }
QString BaseQtVersion::defaultDisplayName(const QString &versionString, const FileName &qmakePath, bool fromPath) { QString location; if (qmakePath.isEmpty()) { location = QCoreApplication::translate("QtVersion", "<unknown>"); } else { // Deduce a description from '/foo/qt-folder/[qtbase]/bin/qmake' -> '/foo/qt-folder'. // '/usr' indicates System Qt 4.X on Linux. QDir dir = qmakePath.toFileInfo().absoluteDir(); do { const QString dirName = dir.dirName(); if (dirName == QLatin1String("usr")) { // System-installed Qt. location = QCoreApplication::translate("QtVersion", "System"); break; } location = dirName; // Also skip default checkouts named 'qt'. Parent dir might have descriptive name. if (dirName.compare(QLatin1String("bin"), Qt::CaseInsensitive) && dirName.compare(QLatin1String("qtbase"), Qt::CaseInsensitive) && dirName.compare(QLatin1String("qt"), Qt::CaseInsensitive)) { break; } } while (!dir.isRoot() && dir.cdUp()); } return fromPath ? QCoreApplication::translate("QtVersion", "Qt %1 in PATH (%2)").arg(versionString, location) : QCoreApplication::translate("QtVersion", "Qt %1 (%2)").arg(versionString, location); }
/** * @brief Remount the file path,the mount point will be found automatically. * @param file the path of file. * @param mountType "rw" or "ro" */ bool RootTools::remount(QString file, QString mountType) { if(mounts_.size() == 0){ mounts_ = getMounts(); } bool foundMount = false; int i = 0; QDir dir = QFileInfo(file).dir(); while(!foundMount){ for(i = 0; i < mounts_.size(); i++){ if(dir.absolutePath() == mounts_[i][1]){ foundMount = true; break; } } dir.cdUp(); if(dir.isRoot()) break; } if(!foundMount) return false; QString cmd = "mount -o " + mountType + ",remount " + mounts_[i][0] + " " + mounts_[i][1] + "\n"; qDebug()<<"mount"<<cmd; shell_.write(cmd.toStdString().c_str(), cmd.size()); shell_.waitForBytesWritten(100); return true; }
void Configuration::readConfig() { if (!m_dirty) return; m_dirty = false; QObject deleterParent; QDir dir = QDir::current(); do { QDir git = dir; if (git.cd(".git")) { m_repoDir = dir; m_repoMetaDataDir = git; QDir refs(git.absoluteFilePath("refs/heads")); m_emptyRepo = refs.count() == 2; // only '.' and '..' break; } if (!dir.cdUp()) break; } while(!dir.isRoot()); QString home = QDir::homePath(); QFile *config; config = new QFile(home + "/.vng/config", &deleterParent); if (! config->exists()) config = new QFile(home + "/.darcs/defaults", &deleterParent); if (config->exists()) { if (! config->open(QIODevice::ReadOnly)) { Logger::error() << "Failed to open config file, is it readable?\n"; return; } char buf[1024]; while(true) { qint64 lineLength = config->readLine(buf, sizeof(buf)); if (lineLength == -1) break; QString line = QString::fromUtf8(buf, lineLength); QString option; if (line.startsWith("ALL ")) option = line.mid(3).trimmed(); else if (line.length() > m_section.length() && line.startsWith(m_section)) option = line.mid(m_section.length()).trimmed(); if (! option.isEmpty()) { const int index = option.indexOf(' '); if (index > 0) { m_options.insert(option.left(index).trimmed(), option.mid(index).trimmed()); } else m_options.insert(option, QString()); } } config->close(); } }
AddressBarButton::AddressBarButton(const QDir &newPath, AddressBar *parent, bool isAbsoluteRoot) : QPushButton(parent) , _path(newPath) , _atLeastOneSubDir(false) , _highlighted(false) , _isAbsoluteRoot(isAbsoluteRoot) , _addressBar(parent) { this->setFlat(true); this->setMouseTracking(true); QDir d = QDir(_path); QDirIterator it(d.absolutePath(), QDir::Dirs | QDir::NoDotAndDotDot); while (it.hasNext()) { it.next(); if (it.fileInfo().isDir()) { _atLeastOneSubDir = true; break; } } // Text + (space + arrow + space) if current dir has subdirectories int width = fontMetrics().width(d.dirName()); if (_atLeastOneSubDir) { // Special root folders like "/" or "D:\" on Windows are empty if (d.isRoot()) { /// XXX? for (QFileInfo fileInfo : QDir::drives()) { if (fileInfo.absolutePath() == _path.absolutePath()) { break; } } if (_isAbsoluteRoot) { width += 10; } else { width += qMax(20, fontMetrics().width(AddressBar::getVolumeInfo(d.absolutePath()))); } } width += 28 + this->height() / 2; this->setMinimumWidth(width); } else { this->setMinimumWidth(width + 18); } this->setText(d.dirName()); this->setMaximumWidth(width); }
void wrapInFunction() { //! [0] QDir("/home/user/Documents") QDir("C:/Documents and Settings") //! [0] //! [1] QDir("images/landscape.png") //! [1] //! [2] QDir("Documents/Letters/Applications").dirName() // "Applications" QDir().dirName() // "." //! [2] //! [3] QDir directory("Documents/Letters"); QString path = directory.filePath("contents.txt"); QString absolutePath = directory.absoluteFilePath("contents.txt"); //! [3] //! [4] QDir dir("example"); if (!dir.exists()) qWarning("Cannot find the example directory"); //! [4] //! [5] QDir dir = QDir::root(); // "/" if (!dir.cd("tmp")) { // "/tmp" qWarning("Cannot find the \"/tmp\" directory"); } else { QFile file(dir.filePath("ex1.txt")); // "/tmp/ex1.txt" if (!file.open(QIODevice::ReadWrite)) qWarning("Cannot create the file %s", file.name()); } //! [5] //! [6] QString bin = "/local/bin"; // where /local/bin is a symlink to /usr/bin QDir binDir(bin); QString canonicalBin = binDir.canonicalPath(); // canonicalBin now equals "/usr/bin" QString ls = "/local/bin/ls"; // where ls is the executable "ls" QDir lsDir(ls); QString canonicalLs = lsDir.canonicalPath(); // canonicalLS now equals "/usr/bin/ls". //! [6] //! [7] QDir dir("/home/bob"); QString s; s = dir.relativeFilePath("images/file.jpg"); // s is "images/file.jpg" s = dir.relativeFilePath("/home/mary/file.txt"); // s is "../mary/file.txt" //! [7] //! [8] QDir::setSearchPaths("icons", QStringList(QDir::homePath() + "/images")); QDir::setSearchPaths("docs", QStringList(":/embeddedDocuments")); ... QPixmap pixmap("icons:undo.png"); // will look for undo.png in QDir::homePath() + "/images" QFile file("docs:design.odf"); // will look in the :/embeddedDocuments resource path //! [8] //! [9] QDir dir("/tmp/root_link"); dir = dir.canonicalPath(); if (dir.isRoot()) qWarning("It is a root link"); //! [9] //! [10] // The current directory is "/usr/local" QDir d1("/usr/local/bin"); QDir d2("bin"); if (d1 == d2) qDebug("They're the same"); //! [10] //! [11] // The current directory is "/usr/local" QDir d1("/usr/local/bin"); d1.setFilter(QDir::Executable); QDir d2("bin"); if (d1 != d2) qDebug("They differ"); //! [11] //! [12] C:/Documents and Settings/Username //! [12] //! [13] Q_INIT_RESOURCE(myapp); //! [13] //! [14] inline void initMyResource() { Q_INIT_RESOURCE(myapp); }
void SchemeReply::List () { QDir dir (url ().toLocalFile ()); if (!dir.exists ()) { setError (ContentNotFoundError, tr ("%1: no such file or directory") .arg (dir.absolutePath ())); emit error (ContentNotFoundError); emit finished (); return; } if (!dir.isReadable ()) { setError (ContentAccessDenied, tr ("Unable to read %1") .arg (dir.absolutePath ())); emit error (ContentAccessDenied); emit finished (); return; } QFile basetplFile (":/plugins/poshuku/plugins/filescheme/basetemplate.tpl"); if (!basetplFile.open (QIODevice::ReadOnly)) { qWarning () << Q_FUNC_INFO << "unable to open" << basetplFile.fileName (); return; } QFile rowtplFile (":/plugins/poshuku/plugins/filescheme/rowtemplate.tpl"); if (!rowtplFile.open (QIODevice::ReadOnly)) { qWarning () << Q_FUNC_INFO << "unable to open" << rowtplFile.fileName (); return; } QString html = basetplFile.readAll (); QString row = rowtplFile.readAll (); QString link = "<a href=\"%1\">%2</a>"; QString rows; if (!dir.isRoot ()) { QIcon icon = qApp->style ()->standardIcon (QStyle::SP_FileDialogToParent); QString addr = QString::fromUtf8 ( QUrl::fromLocalFile ( QFileInfo ( dir.absoluteFilePath ("..") ).canonicalFilePath () ).toEncoded( ) ); rows += row .arg (GetBase64 (icon)) .arg (link .arg (addr) .arg ("..")) .arg (QString ()) .arg (QString ()); } QFileIconProvider iconProvider; const auto& list = dir.entryInfoList (QDir::AllEntries | QDir::Hidden, QDir::Name | QDir::DirsFirst); for (const auto& entry : list) { if (entry.fileName () == "." || entry.fileName () == "..") continue; QUrl urlAddr = QUrl::fromLocalFile (entry.canonicalFilePath ()); QString addr = QString::fromUtf8 (urlAddr.toEncoded ()); QString size; if (entry.isFile ()) size = Util::MakePrettySize (entry.size ()); QString modified = entry.lastModified () .toString (Qt::SystemLocaleShortDate); rows += row .arg (GetBase64 (iconProvider.icon (entry))) .arg (link .arg (addr) .arg (entry.fileName ())) .arg (size) .arg (modified); } html = html .arg (dir.absolutePath ()) .arg (tr ("Contents of %1") .arg (dir.absolutePath ())) .arg (tr ("File")) .arg (tr ("Size")) .arg (tr ("Modified")) .arg (rows); QTextStream stream (&Buffer_); stream << html; stream.flush (); Buffer_.reset (); setHeader (QNetworkRequest::ContentTypeHeader, QByteArray ("text/html")); setHeader (QNetworkRequest::ContentLengthHeader, Buffer_.bytesAvailable ()); setAttribute (QNetworkRequest::HttpStatusCodeAttribute, 200); setAttribute (QNetworkRequest::HttpReasonPhraseAttribute, QByteArray ("OK")); emit metaDataChanged (); emit downloadProgress (Buffer_.size (), Buffer_.size ()); NetworkError errorCode = error (); if (errorCode != NoError) emit error (errorCode); else if (Buffer_.size () > 0) emit readyRead (); emit finished (); }