Prevent crashes when no item is selected and a keyboard shortcut is used

git-svn-id: https://svn.code.sf.net/p/keepassx/code/trunk@148 b624d157-de02-0410-bad0-e51aec6abb33
master
highjinx 17 years ago
parent 961268a67d
commit 5a210a8e30
  1. 21
      src/lib/EntryView.cpp
  2. 4
      src/mainwindow.cpp

@ -131,7 +131,7 @@ void KeepassEntryView::OnHeaderSectionClicked(int index){
} }
void KeepassEntryView::OnSaveAttachment(){ void KeepassEntryView::OnSaveAttachment(){
Q_ASSERT(selectedItems().size()==1); if (selectedItems().size() == 0) return;
CEditEntryDlg::saveAttachment(((EntryViewItem*)selectedItems().first())->EntryHandle,this); CEditEntryDlg::saveAttachment(((EntryViewItem*)selectedItems().first())->EntryHandle,this);
} }
@ -228,7 +228,16 @@ void KeepassEntryView::editEntry(EntryViewItem* item){
void KeepassEntryView::OnNewEntry(){ void KeepassEntryView::OnNewEntry(){
IEntryHandle* NewEntry=db->newEntry(CurrentGroup); IEntryHandle* NewEntry = NULL;
if (!CurrentGroup){// We must be viewing search results. Add the new entry to the first group.
if (db->groups().size() > 0)
NewEntry=db->newEntry(db->sortedGroups()[0]);
else{
QMessageBox::critical(NULL,tr("Error"),tr("At least one group must exist before adding an entry."),tr("OK"));
}
}
else
NewEntry=db->newEntry(CurrentGroup);
CEditEntryDlg dlg(db,NewEntry,this,true); CEditEntryDlg dlg(db,NewEntry,this,true);
if(!dlg.exec()){ if(!dlg.exec()){
db->deleteLastEntry(); db->deleteLastEntry();
@ -257,16 +266,17 @@ void KeepassEntryView::OnEntryActivated(QTreeWidgetItem* item,int Column){
} }
void KeepassEntryView::OnEditEntry(){ void KeepassEntryView::OnEditEntry(){
Q_ASSERT(selectedItems().size()==1); if (selectedItems().size() == 0) return;
editEntry((EntryViewItem*)selectedItems().first()); editEntry((EntryViewItem*)selectedItems().first());
} }
void KeepassEntryView::OnEditOpenUrl(){ void KeepassEntryView::OnEditOpenUrl(){
Q_ASSERT(selectedItems().size()==1); if (selectedItems().size() == 0) return;
openBrowser(((EntryViewItem*)selectedItems().first())->text(logicalColIndex(2))); openBrowser(((EntryViewItem*)selectedItems().first())->text(logicalColIndex(2)));
} }
void KeepassEntryView::OnUsernameToClipboard(){ void KeepassEntryView::OnUsernameToClipboard(){
if (selectedItems().size() == 0) return;
Clipboard->setText(((EntryViewItem*)selectedItems().first())->EntryHandle->username(), QClipboard::Clipboard); Clipboard->setText(((EntryViewItem*)selectedItems().first())->EntryHandle->username(), QClipboard::Clipboard);
if(Clipboard->supportsSelection()){ if(Clipboard->supportsSelection()){
Clipboard->setText(((EntryViewItem*)selectedItems().first())->EntryHandle->username(),QClipboard::Selection); Clipboard->setText(((EntryViewItem*)selectedItems().first())->EntryHandle->username(),QClipboard::Selection);
@ -276,6 +286,7 @@ void KeepassEntryView::OnUsernameToClipboard(){
} }
void KeepassEntryView::OnPasswordToClipboard(){ void KeepassEntryView::OnPasswordToClipboard(){
if (selectedItems().size() == 0) return;
SecString password; SecString password;
password=((EntryViewItem*)selectedItems().first())->EntryHandle->password(); password=((EntryViewItem*)selectedItems().first())->EntryHandle->password();
password.unlock(); password.unlock();
@ -581,7 +592,7 @@ void KeepassEntryView::removeDragItems(){
} }
void KeepassEntryView::OnAutoType(){ void KeepassEntryView::OnAutoType(){
Q_ASSERT(selectedItems().size()==1); if (selectedItems().size() == 0) return;
QString error; QString error;
AutoType::perform(((EntryViewItem*)selectedItems().first())->EntryHandle,error); AutoType::perform(((EntryViewItem*)selectedItems().first())->EntryHandle,error);
} }

@ -652,7 +652,7 @@ switch(EntrySelection){
EditPasswordToClipboardAction->setEnabled(true); EditPasswordToClipboardAction->setEnabled(true);
EditUsernameToClipboardAction->setEnabled(true); EditUsernameToClipboardAction->setEnabled(true);
EditOpenUrlAction->setEnabled(true); EditOpenUrlAction->setEnabled(true);
EditSaveAttachmentAction->setEnabled(true); EditSaveAttachmentAction->setEnabled(((EntryViewItem*)(EntryView->selectedItems()[0]))->EntryHandle->binarySize() > 0);
EditEditEntryAction->setEnabled(true); EditEditEntryAction->setEnabled(true);
EditCloneEntryAction->setEnabled(true); EditCloneEntryAction->setEnabled(true);
EditCloneEntryAction->setText(tr("Clone Entry")); EditCloneEntryAction->setText(tr("Clone Entry"));
@ -692,7 +692,7 @@ switch(EntrySelection){
EditUsernameToClipboardAction->setEnabled(true); EditUsernameToClipboardAction->setEnabled(true);
EditPasswordToClipboardAction->setEnabled(true); EditPasswordToClipboardAction->setEnabled(true);
EditOpenUrlAction->setEnabled(true); EditOpenUrlAction->setEnabled(true);
EditSaveAttachmentAction->setEnabled(true); EditSaveAttachmentAction->setEnabled(((EntryViewItem*)(EntryView->selectedItems()[0]))->EntryHandle->binarySize() > 0);
EditEditEntryAction->setEnabled(true); EditEditEntryAction->setEnabled(true);
EditCloneEntryAction->setEnabled(false); EditCloneEntryAction->setEnabled(false);
EditCloneEntryAction->setText(tr("Clone Entry")); EditCloneEntryAction->setText(tr("Clone Entry"));