Fix crash on creating new file if a specific file type is selected

from the popup menu on the New file icon.
This relates to issue #4.

Signed-off-by: Slávek Banko <slavek.banko@axis.cz>
pull/11/head
Slávek Banko 4 years ago
parent 6fa6e46d9d
commit ae5e2adcf3
No known key found for this signature in database
GPG Key ID: 608F5293A04BE668

@ -40,10 +40,13 @@ public:
bool enabled() const { return m_enabled; } bool enabled() const { return m_enabled; }
void setSubtypesEnabled(bool enabled = true); void setSubtypesEnabled(bool enabled = true);
void addSubtype(const FileType * subtype) { m_subtypes.append(subtype); } void addSubtype(const FileType * subtype) { m_subtypes.append(subtype); }
TQPtrList<FileType> subtypes() const { return m_subtypes; } TQPtrList<FileType> subtypes() const { return m_subtypes; }
void setId(int id) { m_id = id; }
int id() const { return m_id; }
private: private:
TQString m_name; TQString m_name;
TQString m_ext; TQString m_ext;
@ -53,9 +56,10 @@ private:
TQString m_descr; TQString m_descr;
bool m_enabled; bool m_enabled;
TQPtrList<FileType> m_subtypes; TQPtrList<FileType> m_subtypes;
int m_id;
}; };
} }

@ -137,7 +137,7 @@ void FileCreatePart::slotAboutToShowNewPopupMenu()
TDEIcon::DefaultState, NULL, true); TDEIcon::DefaultState, NULL, true);
m_newPopupMenu->insertItem(iconPix, filetype->name(), this, m_newPopupMenu->insertItem(iconPix, filetype->name(), this,
TQT_SLOT(slotNewFilePopup(int)), 0, ++id ); TQT_SLOT(slotNewFilePopup(int)), 0, ++id );
m_newPopupMenu->setItemParameter( id, (long)filetype ); m_newPopupMenu->setItemParameter( id, filetype->id() );
} else } else
{ {
TDEPopupMenu* subMenu = NULL; TDEPopupMenu* subMenu = NULL;
@ -152,7 +152,7 @@ void FileCreatePart::slotAboutToShowNewPopupMenu()
TDEIcon::DefaultState, NULL, true); TDEIcon::DefaultState, NULL, true);
subMenu->insertItem(iconPix, subtype->name(), this, subMenu->insertItem(iconPix, subtype->name(), this,
TQT_SLOT(slotNewFilePopup(int)), 0, ++id ); TQT_SLOT(slotNewFilePopup(int)), 0, ++id );
subMenu->setItemParameter( id, (long)subtype ); subMenu->setItemParameter( id, subtype->id() );
} }
} }
if( subMenu ) if( subMenu )
@ -172,9 +172,9 @@ void FileCreatePart::slotAboutToShowNewPopupMenu()
} }
} }
void FileCreatePart::slotNewFilePopup( int pFileType ) void FileCreatePart::slotNewFilePopup( int fileTypeId )
{ {
const FileType* filetype = (const FileType*) pFileType; const FileType* filetype = getType(fileTypeId);
slotFiletypeSelected( filetype ); slotFiletypeSelected( filetype );
} }
@ -193,10 +193,13 @@ void FileCreatePart::slotProjectOpened() {
void FileCreatePart::addFileType(const TQString & filename) { void FileCreatePart::addFileType(const TQString & filename) {
FileType * filetype = getType(filename); FileType * filetype = getType(filename);
if (!filetype) { if (!filetype) {
FileType* lastFiletype = m_filetypes.last();
int lastTypeId = (lastFiletype && lastFiletype->id() < 0 ? lastFiletype->id() : 0);
filetype = new FileType; filetype = new FileType;
filetype->setName( filename + " files" ); filetype->setName( filename + " files" );
filetype->setExt( filename ); filetype->setExt( filename );
filetype->setCreateMethod("template"); filetype->setCreateMethod("template");
filetype->setId(--lastTypeId);
m_filetypes.append(filetype); m_filetypes.append(filetype);
} }
filetype->setEnabled(true); filetype->setEnabled(true);
@ -212,7 +215,9 @@ void FileCreatePart::slotFiletypeSelected(const FileType * filetype) {
KDevCreateFile::CreatedFile createdFile = createNewFile(filetype->ext(), KDevCreateFile::CreatedFile createdFile = createNewFile(filetype->ext(),
TQString(), TQString(),
TQString(), TQString(),
filetype->subtypeRef()); filetype
? filetype->subtypeRef()
: TQString());
openCreatedFile(createdFile); openCreatedFile(createdFile);
} }
@ -228,6 +233,7 @@ void FileCreatePart::openCreatedFile(const KDevCreateFile::CreatedFile & created
int FileCreatePart::readTypes(const TQDomDocument & dom, TQPtrList<FileType> &m_filetypes, bool enable) { int FileCreatePart::readTypes(const TQDomDocument & dom, TQPtrList<FileType> &m_filetypes, bool enable) {
int numRead = 0; int numRead = 0;
int typeId = 0;
TQDomElement fileTypes = DomUtil::elementByPath(dom,"/kdevfilecreate/filetypes"); TQDomElement fileTypes = DomUtil::elementByPath(dom,"/kdevfilecreate/filetypes");
if (!fileTypes.isNull()) { if (!fileTypes.isNull()) {
for(TQDomNode node = fileTypes.firstChild();!node.isNull();node=node.nextSibling()) { for(TQDomNode node = fileTypes.firstChild();!node.isNull();node=node.nextSibling()) {
@ -242,6 +248,7 @@ int FileCreatePart::readTypes(const TQDomDocument & dom, TQPtrList<FileType> &m_
filetype->setIcon( element.attribute("icon") ); filetype->setIcon( element.attribute("icon") );
filetype->setDescr( (DomUtil::namedChildElement(element, "descr")).text() ); filetype->setDescr( (DomUtil::namedChildElement(element, "descr")).text() );
filetype->setEnabled(enable || (filetype->ext()=="")); filetype->setEnabled(enable || (filetype->ext()==""));
filetype->setId(++typeId);
m_filetypes.append(filetype); m_filetypes.append(filetype);
numRead++; numRead++;
@ -260,6 +267,7 @@ int FileCreatePart::readTypes(const TQDomDocument & dom, TQPtrList<FileType> &m_
subtype->setName( subelement.attribute("name") ); subtype->setName( subelement.attribute("name") );
subtype->setDescr( (DomUtil::namedChildElement(subelement, "descr")).text() ); subtype->setDescr( (DomUtil::namedChildElement(subelement, "descr")).text() );
subtype->setEnabled(enable); subtype->setEnabled(enable);
subtype->setId(++typeId);
filetype->addSubtype(subtype); filetype->addSubtype(subtype);
} }
} }
@ -297,6 +305,25 @@ FileType * FileCreatePart::getType(const TQString & ex, const TQString subtRef)
return NULL; return NULL;
} }
FileType * FileCreatePart::getType(int id) {
TQPtrList<FileType> filetypes = getFileTypes();
for(FileType* filetype = filetypes.first();
filetype;
filetype = filetypes.next())
{
if (filetype->id() == id) return filetype;
TQPtrList<FileType> subtypes = filetype->subtypes();
for(FileType* subtype = subtypes.first();
subtype;
subtype = subtypes.next())
{
if (subtype->id() == id) return subtype;
}
}
return NULL;
}
FileType * FileCreatePart::getEnabledType(const TQString & ex, const TQString subtRef) { FileType * FileCreatePart::getEnabledType(const TQString & ex, const TQString subtRef) {
TQString subtypeRef = subtRef; TQString subtypeRef = subtRef;

@ -67,6 +67,13 @@ public:
* You can omit the subtype and specify the extension as ext-subtype if you wish. * You can omit the subtype and specify the extension as ext-subtype if you wish.
*/ */
FileType * getType(const TQString & ext, const TQString subtype = TQString()); FileType * getType(const TQString & ext, const TQString subtype = TQString());
/**
* Finds the file type object for a given file type or subtype ID.
* IDs for file types are not persistent. ID is a sequence number assigned when
* reading file type definitions from an XML file. Negative numbers are assigned
* for custom file types.
*/
FileType * getType(int id);
/** /**
* Finds the file type object for a given extension and optionally subtype. * Finds the file type object for a given extension and optionally subtype.
* You can omit the subtype and specify the extension as ext-subtype if you wish. * You can omit the subtype and specify the extension as ext-subtype if you wish.
@ -93,9 +100,9 @@ public slots:
/** /**
* Called from TDEToolBarPopupMenu to request a new file action * Called from TDEToolBarPopupMenu to request a new file action
* @param pFileType is acutally a pointer to FileType * @param fileTypeId is a sequence number that identifies a particular FileType
*/ */
void slotNewFilePopup(int pFileType); void slotNewFilePopup(int fileTypeId);
protected slots: protected slots:
void slotNoteFiletype(const FileType * filetype); void slotNoteFiletype(const FileType * filetype);

Loading…
Cancel
Save