- // Parts of the following code have been taken
- // from the class KonqPopupMenu located in
- // libqonq/konq_popupmenu.h of Konqueror.
- // (Copyright (C) 2000 David Faure <faure@kde.org>,
- // Copyright (C) 2001 Holger Freyther <freyther@yahoo.com>)
-
- clearStatusBar();
-
- DolphinStatusBar* statusBar = m_activeView->statusBar();
- const KUrl baseUrl(m_activeView->url());
-
- QString name(i18n("New Folder"));
- baseUrl.path(KUrl::AddTrailingSlash);
-
-
- if (baseUrl.isLocalFile() && QFileInfo(baseUrl.path(KUrl::AddTrailingSlash) + name).exists()) {
- name = KIO::RenameDlg::suggestName(baseUrl, i18n("New Folder"));
- }
-
- bool ok = false;
- name = KInputDialog::getText(i18n("New Folder"),
- i18n("Enter folder name:" ),
- name,
- &ok,
- this);
-
- if (!ok) {
- // the user has pressed 'Cancel'
- return;
- }
-
- assert(!name.isEmpty());
-
- KUrl url;
- if ((name[0] == '/') || (name[0] == '~')) {
- url.setPath(KShell::tildeExpand(name));
- }
- else {
- name = KIO::encodeFileName(name);
- url = baseUrl;
- url.addPath(name);
- }
- ok = KIO::NetAccess::mkdir(url, this);
-
- // TODO: provide message type hint
- if (ok) {
- statusBar->setMessage(i18n("Created folder %1.",url.path()),
- DolphinStatusBar::OperationCompleted);
-
- DolphinCommand command(DolphinCommand::CreateFolder, KUrl::List(), url);
- UndoManager::instance().addCommand(command);
- }
- else {
- // Creating of the folder has been failed. Check whether the creating
- // has been failed because a folder with the same name exists...
- if (KIO::NetAccess::exists(url, true, this)) {
- statusBar->setMessage(i18n("A folder named %1 already exists.",url.path()),
- DolphinStatusBar::Error);
- }
- else {
- statusBar->setMessage(i18n("Creating of folder %1 failed.",url.path()),
- DolphinStatusBar::Error);
- }
-
- }
-}
-
-void DolphinMainWindow::createFile()
-{
- // Parts of the following code have been taken
- // from the class KonqPopupMenu located in
- // libqonq/konq_popupmenu.h of Konqueror.
- // (Copyright (C) 2000 David Faure <faure@kde.org>,
- // Copyright (C) 2001 Holger Freyther <freyther@yahoo.com>)
-
- clearStatusBar();
-
- // TODO: const Entry& entry = m_createFileTemplates[QString(sender->name())];
- // should be enough. Anyway: the implemantation of [] does a linear search internally too.
- KSortableList<CreateFileEntry, QString>::ConstIterator it = m_createFileTemplates.begin();
- KSortableList<CreateFileEntry, QString>::ConstIterator end = m_createFileTemplates.end();
-
- const QString senderName(sender()->objectName());
- bool found = false;
- CreateFileEntry entry;
- while (!found && (it != end)) {
- if ((*it).index() == senderName) {
- entry = (*it).value();
- found = true;
- }
- else {
- ++it;
- }
- }
-
- DolphinStatusBar* statusBar = m_activeView->statusBar();
- if (!found || !QFile::exists(entry.templatePath)) {
- statusBar->setMessage(i18n("Could not create file."), DolphinStatusBar::Error);
- return;
- }
-
- // Get the source path of the template which should be copied.
- // The source path is part of the Url entry of the desktop file.
- const int pos = entry.templatePath.findRev('/');
- QString sourcePath(entry.templatePath.left(pos + 1));
- sourcePath += KDesktopFile(entry.templatePath, true).readPathEntry("Url");
-
- QString name(i18n(entry.name.toAscii()));
- // Most entry names end with "..." (e. g. "HTML File..."), which is ok for
- // menus but no good choice for a new file name -> remove the dots...
- name.replace("...", QString::null);
-
- // add the file extension to the name
- name.append(sourcePath.right(sourcePath.length() - sourcePath.findRev('.')));
-
- // Check whether a file with the current name already exists. If yes suggest automatically
- // a unique file name (e. g. "HTML File" will be replaced by "HTML File_1").
- const KUrl viewUrl(m_activeView->url());
- const bool fileExists = viewUrl.isLocalFile() &&
- QFileInfo(viewUrl.path(KUrl::AddTrailingSlash) + KIO::encodeFileName(name)).exists();
- if (fileExists) {
- name = KIO::RenameDlg::suggestName(viewUrl, name);
- }
-
- // let the user change the suggested file name
- bool ok = false;
- name = KInputDialog::getText(entry.name,
- entry.comment,
- name,
- &ok,
- this);
- if (!ok) {
- // the user has pressed 'Cancel'
- return;
- }
-
- // before copying the template to the destination path check whether a file
- // with the given name already exists
- const QString destPath(viewUrl.pathOrUrl() + "/" + KIO::encodeFileName(name));
- const KUrl destUrl(destPath);
- if (KIO::NetAccess::exists(destUrl, false, this)) {
- statusBar->setMessage(i18n("A file named %1 already exists.",name),
- DolphinStatusBar::Error);
- return;
- }
-
- // copy the template to the destination path
- const KUrl sourceUrl(sourcePath);
- KIO::CopyJob* job = KIO::copyAs(sourceUrl, destUrl);
- job->setDefaultPermissions(true);
- if (KIO::NetAccess::synchronousRun(job, this)) {
- statusBar->setMessage(i18n("Created file %1.",name),
- DolphinStatusBar::OperationCompleted);
-
- KUrl::List list;
- list.append(sourceUrl);
- DolphinCommand command(DolphinCommand::CreateFile, list, destUrl);
- UndoManager::instance().addCommand(command);
-
- }
- else {
- statusBar->setMessage(i18n("Creating of file %1 failed.",name),
- DolphinStatusBar::Error);
- }