summaryrefslogtreecommitdiffstats
path: root/parts/filecreate/filecreate_newfile.cpp
blob: 2077556ca08b8d13b11f4303c8772c2f32997b5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/***************************************************************************
 *   Copyright (C) 2003 by Julian Rockey                                   *
 *   linux@jrockey.com                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "filecreate_newfile.h"

#include "filecreate_newfile.moc"

#include <qvbox.h>
#include <qgrid.h>

#include <qhbox.h>
#include <qlayout.h>
#include <qcheckbox.h>
#include <qlabel.h>
#include <klineedit.h>
#include <kurlrequester.h>
#include <kcombobox.h>
#include <klocale.h>
#include <kdebug.h>
#include <kmessagebox.h>


namespace FileCreate {

  NewFileChooser::NewFileChooser(QWidget * parent) :
    KDialogBase(KDialogBase::Plain, i18n("New file dialog (title)", "New File"), KDialogBase::Ok|KDialogBase::Cancel,
                KDialogBase::Ok, parent, "New file", true)
  {
      QVBoxLayout* lay = new QVBoxLayout( plainPage(), 5, 5 );

      lay->addWidget( new QLabel( i18n("<b>New File Creation</b>"), plainPage() ) );

      QGridLayout* grid = new QGridLayout(lay, 2, 2, 5 );
      QLabel * l = new QLabel(i18n("&Directory:"), plainPage() );
      grid->addWidget(l, 0, 0);
      m_urlreq = new KURLRequester( plainPage(), "url request" );
      grid->addWidget(m_urlreq, 0, 1);
      l->setBuddy(m_urlreq);
      l = new QLabel(i18n("&File name:"), plainPage() );
      grid->addWidget(l, 1, 0);
      m_filename = new KLineEdit( plainPage() );
      grid->addWidget(m_filename, 1, 1);
      l->setBuddy(m_filename);
//      lay->addWidget( grid );

      QHBoxLayout* hbox = new QHBoxLayout( lay, 5 );
      m_filetypes = new KComboBox( plainPage(), "combo" );
      hbox->addWidget(m_filetypes);
      m_addToProject = new QCheckBox( i18n("Add to project (on checkbox)", "&Add to project"), plainPage(), "addproject" );
      hbox->addWidget(m_addToProject);

      lay->addStretch(20);

      m_filename->setFocus();
      m_addToProject->setChecked( true );

      m_urlreq->setMode((int) KFile::Directory);
      connect( m_filename,  SIGNAL( textChanged ( const QString & ) ), this, SLOT( slotFileNameChanged(const QString & ) ) );
      slotFileNameChanged( m_filename->text() );
  }

  NewFileChooser::~NewFileChooser() {
  }

    void NewFileChooser::slotFileNameChanged(const QString & _text)
    {
        enableButtonOK( !_text.isEmpty() );
    }

  void NewFileChooser::setFileTypes(QPtrList<FileType> filetypes) {
    for(FileType * filetype = filetypes.first();
	filetype;
	filetype=filetypes.next()) {

      if (filetype->enabled()) {

	if (filetype->subtypes().count()==0)
          addType(filetype);

	QPtrList<FileType> subtypes = filetype->subtypes();
	for(FileType * subtype = subtypes.first();
	    subtype;
	    subtype=subtypes.next()) {
	  if (subtype->enabled())
            addType(subtype);

	}

      }

    }

  }

  KURL NewFileChooser::url() const {

    KURL result ( m_urlreq->url() );
    result.cd( m_filename->text() );
    return result;
  }

  bool NewFileChooser::addToProject() const {
    return m_addToProject->isChecked();
  }

  const FileType *NewFileChooser::selectedType() const {
    if (!m_filetypes->count()) return NULL;
    return m_typeInCombo[m_filetypes->currentItem()];
  }

  void NewFileChooser::addType(const FileType * filetype) {
    m_typeInCombo[m_filetypes->count()]=filetype;
    m_filetypes->insertItem( filetype->name() +
                             (filetype->ext()!="" ? QString(" (." + filetype->ext() + ")") : QString("") ) );
  }

  void NewFileChooser::setCurrent(const FileType *filetype) {
    int changeToRow = -1;
    QMap<int,const FileType*>::Iterator it;
    for ( it = m_typeInCombo.begin(); it != m_typeInCombo.end() && changeToRow==-1; ++it ) {
      if (it.data()==filetype)
        changeToRow=it.key();
    }
    if (changeToRow>-1) m_filetypes->setCurrentItem(changeToRow);
  }

  void NewFileChooser::setDirectory(const QString & url) {
    m_urlreq->setURL(url);
  }

  void NewFileChooser::setName(const QString & name) {
    m_filename->setText(name);
  }

  void NewFileChooser::setInProjectMode( bool m )
  {
    m_addToProject->setEnabled(m);
    m_addToProject->setChecked(m);
  }

	void NewFileChooser::accept()
	{
		QString fullPath = url().path();
		if ( !selectedType()->ext().isEmpty() && !fullPath.endsWith("." + selectedType()->ext())) fullPath+="." + selectedType()->ext();
		QFileInfo file( fullPath );
		if ( file.exists() )
		{
			KMessageBox::sorry( this, i18n("A file with this name already exists"), i18n("File Exists") );
			return;
		}
		KDialogBase::accept();
	}

}