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

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

@ -67,6 +67,13 @@ public:
* You can omit the subtype and specify the extension as ext-subtype if you wish.
*/
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.
* 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
* @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:
void slotNoteFiletype(const FileType * filetype);

Loading…
Cancel
Save