23#include <tdedesktopfile.h> 
   25#include <tdeinstance.h> 
   26#include <kinputdialog.h> 
   28#include <tdemessagebox.h> 
   29#include <tdestandarddirs.h> 
   30#include <kprotocolinfo.h> 
   31#include <tdepopupmenu.h> 
   35#include <tdeio/renamedlg.h> 
   37#include <kpropertiesdialog.h> 
   38#include "konq_operations.h" 
   47#include <kurlrequester.h> 
   49#include <tqpopupmenu.h> 
   51TQValueList<KNewMenu::Entry> * KNewMenu::s_templatesList = 0L;
 
   52int KNewMenu::s_templatesVersion = 0;
 
   53bool KNewMenu::s_filesParsed = 
false;
 
   54KDirWatch * KNewMenu::s_pDirWatch = 0L;
 
   56class KNewMenu::KNewMenuPrivate
 
   59    KNewMenuPrivate() : m_parentWidget(0) {}
 
   60    TDEActionCollection * m_actionCollection;
 
   62    TQWidget *m_parentWidget;
 
   63    TDEActionMenu *m_menuDev;
 
   67  TDEActionMenu( i18n( 
"Create New" ), 
"document-new", _collec, name ),
 
   73    d = 
new KNewMenuPrivate;
 
   74    d->m_actionCollection = _collec;
 
   78KNewMenu::KNewMenu( TDEActionCollection * _collec, TQWidget *parentWidget, 
const char *name ) :
 
   79  TDEActionMenu( i18n( 
"Create New" ), 
"document-new", _collec, name ),
 
   82    d = 
new KNewMenuPrivate;
 
   83    d->m_actionCollection = _collec;
 
   84    d->m_parentWidget = parentWidget;
 
   94void KNewMenu::makeMenus()
 
   96    d->m_menuDev = 
new TDEActionMenu( i18n( 
"Link to Device" ), 
"kcmdevices", d->m_actionCollection, 
"devnew" );
 
  104    if (menuItemsVersion < s_templatesVersion || s_templatesVersion == 0)
 
  109        TQValueList<TDEAction*> actions = d->m_actionCollection->actions( 
"KNewMenu" );
 
  110        for( TQValueListIterator<TDEAction*> it = actions.begin(); it != actions.end(); ++it )
 
  113            d->m_actionCollection->remove( *it );
 
  116        if (!s_templatesList) { 
 
  117            s_templatesList = 
new TQValueList<Entry>();
 
  124        if ( !s_filesParsed )
 
  129        menuItemsVersion = s_templatesVersion;
 
  133void KNewMenu::parseFiles()
 
  136    s_filesParsed = 
true;
 
  137    TQValueList<Entry>::Iterator templ = s_templatesList->begin();
 
  138    for ( ; templ != s_templatesList->end(); ++templ)
 
  141        TQString filePath = (*templ).filePath;
 
  142        if ( !filePath.isEmpty() )
 
  145            TQString templatePath;
 
  148            if ( TDEDesktopFile::isDesktopFile( filePath ) ) {
 
  149                TDESimpleConfig config( filePath, 
true );
 
  150                config.setDesktopGroup();
 
  151                text = config.readEntry(
"Name");
 
  152                (*templ).icon = config.readEntry(
"Icon");
 
  153                (*templ).comment = config.readEntry(
"Comment");
 
  154                TQString type = config.readEntry( 
"Type" );
 
  155                if ( type == 
"Link" )
 
  157                    templatePath = config.readPathEntry(
"URL");
 
  158                    if ( templatePath[0] != 
'/' )
 
  160                        if ( templatePath.startsWith(
"file:/") )
 
  161                            templatePath = KURL(templatePath).path();
 
  165                            TQString linkDir = filePath.left( filePath.findRev( 
'/' ) + 1  );
 
  167                            templatePath = linkDir + templatePath;
 
  171                if ( templatePath.isEmpty() )
 
  174                    (*templ).entryType = TEMPLATE;
 
  175                    (*templ).templatePath = (*templ).filePath; 
 
  177                    (*templ).entryType = LINKTOTEMPLATE;
 
  178                    (*templ).templatePath = templatePath;
 
  184                text = KURL(filePath).fileName();
 
  185                if ( text.endsWith(
".desktop") )
 
  186                    text.truncate( text.length() - 8 );
 
  187                else if ( text.endsWith(
".kdelnk") )
 
  188                    text.truncate( text.length() - 7 );
 
  190            (*templ).text = text;
 
  196            (*templ).entryType = SEPARATOR;
 
  201void KNewMenu::fillMenu()
 
  204    popupMenu()->clear();
 
  205    d->m_menuDev->popupMenu()->clear();
 
  207    TDEAction *linkURL = 0, *linkApp = 0;  
 
  210    TQValueList<Entry>::Iterator templ = s_templatesList->begin();
 
  211    for ( ; templ != s_templatesList->end(); ++templ, ++i)
 
  213        if ( (*templ).entryType != SEPARATOR )
 
  223            TQValueList<TDEAction*> actions = d->m_actionCollection->actions();
 
  224            TQValueListIterator<TDEAction*> it = actions.begin();
 
  225            for( ; it != actions.end() && !bSkip; ++it )
 
  227                if ( (*it)->text() == (*templ).text )
 
  229                    kdDebug(1203) << 
"KNewMenu: skipping " << (*templ).filePath << endl;
 
  236                Entry entry = *(s_templatesList->at( i-1 ));
 
  239                if ( (*templ).templatePath.endsWith( 
"emptydir" ) )
 
  241                    TDEAction * act = 
new TDEAction( (*templ).text, (*templ).icon, 0, 
this, TQ_SLOT( 
slotNewDir() ),
 
  242                                     d->m_actionCollection, TQCString().sprintf(
"newmenu%d", i ) );
 
  243                    act->setGroup( 
"KNewMenu" );
 
  244                    act->plug( popupMenu() );
 
  246                    TDEActionSeparator *sep = 
new TDEActionSeparator();
 
  247                    sep->plug( popupMenu() );
 
  251                    TDEAction * act = 
new TDEAction( (*templ).text, (*templ).icon, 0, 
this, TQ_SLOT( 
slotNewFile() ),
 
  252                                             d->m_actionCollection, TQCString().sprintf(
"newmenu%d", i ) );
 
  253                    act->setGroup( 
"KNewMenu" );
 
  255                    if ( (*templ).templatePath.endsWith( 
"URL.desktop" ) )
 
  259                    else if ( (*templ).templatePath.endsWith( 
"Program.desktop" ) )
 
  263                    else if ( TDEDesktopFile::isDesktopFile( entry.templatePath ) )
 
  265                        TDEDesktopFile df( entry.templatePath );
 
  266                        if(df.readType() == 
"FSDevice")
 
  267                            act->plug( d->m_menuDev->popupMenu() );
 
  269                          act->plug( popupMenu() );
 
  273                        act->plug( popupMenu() );
 
  278            Q_ASSERT( (*templ).entryType != 0 );
 
  280            TDEActionSeparator * act = 
new TDEActionSeparator();
 
  281            act->plug( popupMenu() );
 
  285    TDEActionSeparator * act = 
new TDEActionSeparator();
 
  286    act->plug( popupMenu() );
 
  287    if ( linkURL ) linkURL->plug( popupMenu() );
 
  288    if ( linkApp ) linkApp->plug( popupMenu() );
 
  289    d->m_menuDev->plug( popupMenu() );
 
  298        s_pDirWatch = 
new KDirWatch;
 
  299        TQStringList dirs = d->m_actionCollection->instance()->dirs()->resourceDirs(
"templates");
 
  300        for ( TQStringList::Iterator it = dirs.begin() ; it != dirs.end() ; ++it )
 
  303            s_pDirWatch->addDir( *it );
 
  305        connect ( s_pDirWatch, TQ_SIGNAL( dirty( 
const TQString & ) ),
 
  307        connect ( s_pDirWatch, TQ_SIGNAL( created( 
const TQString & ) ),
 
  309        connect ( s_pDirWatch, TQ_SIGNAL( deleted( 
const TQString & ) ),
 
  313    s_templatesVersion++;
 
  314    s_filesParsed = 
false;
 
  316    s_templatesList->clear();
 
  319    TQStringList files = d->m_actionCollection->instance()->dirs()->findAllResources(
"templates");
 
  320    KSortableValueList<Entry,TQString> slist;
 
  321    for ( TQStringList::Iterator it = files.begin() ; it != files.end() ; ++it )
 
  324        if ( (*it)[0] != 
'.' )
 
  330            if ( (*it).endsWith( 
"Directory.desktop" ) ||
 
  331                 (*it).endsWith( 
"linkProgram.desktop" ) ||
 
  332                 (*it).endsWith( 
"linkURL.desktop" ) )
 
  333                s_templatesList->prepend( e );
 
  336                TDESimpleConfig config( *it, 
true );
 
  337                config.setDesktopGroup();
 
  341                TQString key = config.readEntry(
"Name");
 
  342                if ( (*it).endsWith( 
"TextFile.desktop" ) )
 
  347                slist.insert( key, e );
 
  352    for(KSortableValueList<Entry, TQString>::ConstIterator it = slist.begin(); it != slist.end(); ++it)
 
  354        s_templatesList->append( (*it).value() );
 
  363    if (popupFiles.isEmpty())
 
  371    int id = TQString( sender()->name() + 7 ).toInt(); 
 
  375    KRun::runCommand(TQString(sender()->name()));
 
  381    Entry entry = *(s_templatesList->at( 
id - 1 ));
 
  384    if ( !TQFile::exists( entry.templatePath ) ) {
 
  385        kdWarning(1203) << entry.templatePath << 
" doesn't exist" << endl;
 
  386        KMessageBox::sorry( 0L, i18n(
"<qt>The template file <b>%1</b> does not exist.</qt>").arg(entry.templatePath));
 
  389    m_isURLDesktopFile = 
false;
 
  391    if ( TDEDesktopFile::isDesktopFile( entry.templatePath ) )
 
  393    TDEDesktopFile df( entry.templatePath );
 
  395        if ( df.readType() == 
"Link" )
 
  397            m_isURLDesktopFile = 
true;
 
  399            KURLDesktopFileDlg dlg( i18n(
"File name:"), entry.comment, d->m_parentWidget );
 
  403                name = dlg.fileName();
 
  404                m_linkURL = dlg.url();
 
  405                if ( name.isEmpty() || m_linkURL.isEmpty() )
 
  407                if ( !name.endsWith( 
".desktop" ) )
 
  415            KURL::List::Iterator it = popupFiles.begin();
 
  416            for ( ; it != popupFiles.end(); ++it )
 
  421                TQString text = entry.text;
 
  422                text.replace( 
"...", TQString() ); 
 
  424        KURL defaultFile( *it );
 
  425        defaultFile.addPath( TDEIO::encodeFileName( text ) );
 
  426        if ( defaultFile.isLocalFile() && TQFile::exists( defaultFile.path() ) )
 
  427            text = TDEIO::RenameDlg::suggestName( *it, text);
 
  430                templateURL.setPath( entry.templatePath );
 
  431                (void) 
new KPropertiesDialog( templateURL, *it, text, d->m_parentWidget );
 
  441        TQString text = entry.text;
 
  442        text.replace( 
"...", TQString() ); 
 
  444    KURL defaultFile( *(popupFiles.begin()) );
 
  445    defaultFile.addPath( TDEIO::encodeFileName( text ) );
 
  446    if ( defaultFile.isLocalFile() && TQFile::exists( defaultFile.path() ) )
 
  447        text = TDEIO::RenameDlg::suggestName( *(popupFiles.begin()), text);
 
  449        name = KInputDialog::getText( TQString::null, entry.comment,
 
  450        text, &ok, d->m_parentWidget );
 
  457    KURL::List::Iterator it = popupFiles.begin();
 
  459    TQString src = entry.templatePath;
 
  460    for ( ; it != popupFiles.end(); ++it )
 
  463        dest.addPath( TDEIO::encodeFileName(name) ); 
 
  464        d->m_destPath = dest.path(); 
 
  469        TDEIO::CopyJob * job = TDEIO::copyAs( uSrc, dest );
 
  470        job->setDefaultPermissions( 
true );
 
  471        connect( job, TQ_SIGNAL( result( TDEIO::Job * ) ),
 
  472                TQ_SLOT( slotResult( TDEIO::Job * ) ) );
 
  473        if ( m_isURLDesktopFile )
 
  474        connect( job, TQ_SIGNAL( renamed( TDEIO::Job *, 
const KURL&, 
const KURL& ) ),
 
  475                 TQ_SLOT( slotRenamed( TDEIO::Job *, 
const KURL&, 
const KURL& ) ) );
 
  478        (void)
new KonqCommandRecorder( KonqCommand::COPY, lst, dest, job );
 
  484void KNewMenu::slotRenamed( TDEIO::Job *, 
const KURL& from , 
const KURL& to )
 
  486    if ( from.isLocalFile() )
 
  488        kdDebug() << k_funcinfo << from.prettyURL() << 
" -> " << to.prettyURL() << 
" ( m_destPath=" << d->m_destPath << 
")" << endl;
 
  489        Q_ASSERT( from.path() == d->m_destPath );
 
  490        d->m_destPath = to.path();
 
  494void KNewMenu::slotResult( TDEIO::Job * job )
 
  497        job->showErrorDialog();
 
  500        KURL destURL = 
static_cast<TDEIO::CopyJob*
>(job)->destURL();
 
  501        if ( destURL.isLocalFile() )
 
  503            if ( m_isURLDesktopFile )
 
  507                kdDebug(1203) << 
" destURL=" << destURL.path() << 
" " << 
" d->m_destPath=" << d->m_destPath << endl;
 
  508                TDEDesktopFile df( d->m_destPath );
 
  509                df.writeEntry( 
"Icon", KProtocolInfo::icon( KURL(m_linkURL).protocol() ) );
 
  510                df.writePathEntry( 
"URL", m_linkURL );
 
  516                (void) ::utime( TQFile::encodeName( destURL.path() ), 0 );
 
  524KURLDesktopFileDlg::KURLDesktopFileDlg( 
const TQString& textFileName, 
const TQString& textUrl )
 
  525    : KDialogBase( Plain, TQString::null, Ok|Cancel|User1, Ok, 0L , 0L, true,
 
  526                   true, KStdGuiItem::clear() )
 
  528    initDialog( textFileName, TQString::null, textUrl, TQString::null );
 
  531KURLDesktopFileDlg::KURLDesktopFileDlg( 
const TQString& textFileName, 
const TQString& textUrl, TQWidget *parent )
 
  532    : KDialogBase( Plain, TQString::null, Ok|Cancel|User1, Ok, parent, 0L, true,
 
  533                   true, KStdGuiItem::clear() )
 
  535    initDialog( textFileName, TQString::null, textUrl, TQString::null );
 
  538void KURLDesktopFileDlg::initDialog( 
const TQString& textFileName, 
const TQString& defaultName, 
const TQString& textUrl, 
const TQString& defaultUrl )
 
  540    TQVBoxLayout * topLayout = 
new TQVBoxLayout( plainPage(), 0, spacingHint() );
 
  543    TQHBox * fileNameBox = 
new TQHBox( plainPage() );
 
  544    topLayout->addWidget( fileNameBox );
 
  546    TQLabel * label = 
new TQLabel( textFileName, fileNameBox );
 
  547    m_leFileName = 
new KLineEdit( fileNameBox, 0L );
 
  548    m_leFileName->setMinimumWidth(m_leFileName->sizeHint().width() * 3);
 
  549    label->setBuddy(m_leFileName);  
 
  550    m_leFileName->setText( defaultName );
 
  551    m_leFileName->setSelection(0, m_leFileName->text().length()); 
 
  552    connect( m_leFileName, TQ_SIGNAL(textChanged(
const TQString&)),
 
  553             TQ_SLOT(slotNameTextChanged(
const TQString&)) );
 
  556    TQHBox * urlBox = 
new TQHBox( plainPage() );
 
  557    topLayout->addWidget( urlBox );
 
  558    label = 
new TQLabel( textUrl, urlBox );
 
  559    m_urlRequester = 
new KURLRequester( defaultUrl, urlBox, 
"urlRequester" );
 
  560    m_urlRequester->setMode( KFile::File | KFile::Directory );
 
  562    m_urlRequester->setMinimumWidth( m_urlRequester->sizeHint().width() * 3 );
 
  563    connect( m_urlRequester->lineEdit(), TQ_SIGNAL(textChanged(
const TQString&)),
 
  564             TQ_SLOT(slotURLTextChanged(
const TQString&)) );
 
  565    label->setBuddy(m_urlRequester);  
 
  567    m_urlRequester->setFocus();
 
  568    enableButtonOK( !defaultName.isEmpty() && !defaultUrl.isEmpty() );
 
  569    connect( 
this, TQ_SIGNAL(user1Clicked()), 
this, TQ_SLOT(slotClear()) );
 
  570    m_fileNameEdited = 
false;
 
  573TQString KURLDesktopFileDlg::url()
 const 
  575    if ( result() == TQDialog::Accepted )
 
  576        return m_urlRequester->url();
 
  578        return TQString::null;
 
  581TQString KURLDesktopFileDlg::fileName()
 const 
  583    if ( result() == TQDialog::Accepted )
 
  584        return m_leFileName->text();
 
  586        return TQString::null;
 
  589void KURLDesktopFileDlg::slotClear()
 
  591    m_leFileName->setText( TQString::null );
 
  592    m_urlRequester->clear();
 
  593    m_fileNameEdited = 
false;
 
  596void KURLDesktopFileDlg::slotNameTextChanged( 
const TQString& )
 
  598    kdDebug() << k_funcinfo << endl;
 
  599    m_fileNameEdited = 
true;
 
  600    enableButtonOK( !m_leFileName->text().isEmpty() && !m_urlRequester->url().isEmpty() );
 
  603void KURLDesktopFileDlg::slotURLTextChanged( 
const TQString& )
 
  605    if ( !m_fileNameEdited )
 
  610        KURL url( m_urlRequester->url() );
 
  611        if ( KProtocolInfo::supportsListing( url ) )
 
  612            m_leFileName->setText( url.fileName() );
 
  614            m_leFileName->setText( url.url() );
 
  615        m_fileNameEdited = 
false; 
 
  617    enableButtonOK( !m_leFileName->text().isEmpty() && !m_urlRequester->url().isEmpty() );
 
  621#include "knewmenu.moc" 
static void newDir(TQWidget *parent, const KURL &baseURL)
Ask for the name of a new directory and create it.