- // 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).key() == 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.lastIndexOf('/');
- 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.lastIndexOf('.')));
-
- // 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);
- }