@@ -210,7 +210,6 @@ namespace FdoSecrets
210210 m_collections.reserve (colls.size ());
211211 for (const auto & coll : asConst (colls)) {
212212 m_collections << coll;
213- connect (coll, &Collection::doneUnlockCollection, this , &UnlockPrompt::collectionUnlockFinished);
214213 }
215214 for (const auto & item : asConst (items)) {
216215 m_items[item->collection ()] << item;
@@ -234,6 +233,7 @@ namespace FdoSecrets
234233 bool waitingForCollections = false ;
235234 for (const auto & c : asConst (m_collections)) {
236235 if (c) {
236+ connect (c, &Collection::doneUnlockCollection, this , &UnlockPrompt::collectionUnlockFinished);
237237 // doUnlock is nonblocking, execution will continue in collectionUnlockFinished
238238 // it is ok to call doUnlock multiple times before it's actually unlocked by the user
239239 c->doUnlock ();
@@ -242,7 +242,7 @@ namespace FdoSecrets
242242 }
243243
244244 // unlock items directly if no collection unlocking pending
245- // o.w. do it in collectionUnlockFinished
245+ // o.w. doing it in collectionUnlockFinished
246246 if (!waitingForCollections) {
247247 unlockItems ();
248248 }
@@ -400,18 +400,49 @@ namespace FdoSecrets
400400 return PromptResult::accepted (false );
401401 }
402402
403- bool locked = true ;
404- auto ret = m_coll->locked (locked);
405- if (locked) {
406- // collection was locked
407- return DBusResult{DBUS_ERROR_SECRET_IS_LOCKED};
408- }
409-
410403 // save a weak reference to the client which may be used asynchronously later
411404 m_client = client;
412405
406+ // give the user a chance to unlock the collection
407+ // UnlockPrompt will handle the case of collection already unlocked
408+ auto prompt = PromptBase::Create<UnlockPrompt>(service (), QSet<Collection*>{m_coll.data ()}, QSet<Item*>{});
409+ if (!prompt) {
410+ return DBusResult{QDBusError::InternalError};
411+ }
412+ // postpone anything after the prompt
413+ connect (prompt, &PromptBase::completed, this , [this , windowId](bool dismissed) {
414+ if (dismissed) {
415+ finishPrompt (dismissed);
416+ } else {
417+ auto res = createItem (windowId);
418+ if (res.err ()) {
419+ qWarning () << " FdoSecrets:" << res;
420+ finishPrompt (true );
421+ }
422+ }
423+ });
424+
425+ auto ret = prompt->prompt (client, windowId);
426+ if (ret.err ()) {
427+ return ret;
428+ }
429+ return PromptResult::Pending;
430+ }
431+
432+ DBusResult CreateItemPrompt::createItem (const QString& windowId)
433+ {
434+ auto client = m_client.lock ();
435+ if (!client) {
436+ // client already gone
437+ return {};
438+ }
439+
440+ if (!m_coll) {
441+ return DBusResult{DBUS_ERROR_SECRET_NO_SUCH_OBJECT};
442+ }
443+
413444 // get itemPath to create item and
414- // try finding an existing item using attributes
445+ // try to find an existing item using attributes
415446 QString itemPath{};
416447 auto iterAttr = m_properties.find (DBUS_INTERFACE_SECRET_ITEM + " .Attributes" );
417448 if (iterAttr != m_properties.end ()) {
@@ -425,7 +456,7 @@ namespace FdoSecrets
425456
426457 // check existing item using attributes
427458 QList<Item*> existing;
428- ret = m_coll->searchItems (client, attributes, existing);
459+ auto ret = m_coll->searchItems (client, attributes, existing);
429460 if (ret.err ()) {
430461 return ret;
431462 }
@@ -444,31 +475,29 @@ namespace FdoSecrets
444475 }
445476
446477 // the item may be locked due to authorization
447- ret = m_item->locked (client, locked);
448- if (ret.err ()) {
449- return ret;
450- }
451- if (locked) {
452- // give the user a chance to unlock the item
453- auto prompt = PromptBase::Create<UnlockPrompt>(service (), QSet<Collection*>{}, QSet<Item*>{m_item});
454- if (!prompt) {
455- return DBusResult{QDBusError::InternalError};
456- }
457- // postpone anything after the confirmation
458- connect (prompt, &PromptBase::completed, this , [this ]() {
478+ // give the user a chance to unlock the item
479+ auto prompt = PromptBase::Create<UnlockPrompt>(service (), QSet<Collection*>{}, QSet<Item*>{m_item});
480+ if (!prompt) {
481+ return DBusResult{QDBusError::InternalError};
482+ }
483+ // postpone anything after the confirmation
484+ connect (prompt, &PromptBase::completed, this , [this ](bool dismissed) {
485+ if (dismissed) {
486+ finishPrompt (dismissed);
487+ } else {
459488 auto res = updateItem ();
460- finishPrompt (res.err ());
461- });
462-
463- ret = prompt->prompt (client, windowId);
464- if (ret.err ()) {
465- return ret;
489+ if (res.err ()) {
490+ qWarning () << " FdoSecrets:" << res;
491+ finishPrompt (true );
492+ }
466493 }
467- return PromptResult::Pending;
468- }
494+ });
469495
470- // the item can be updated directly
471- return updateItem ();
496+ auto ret = prompt->prompt (client, windowId);
497+ if (ret.err ()) {
498+ return ret;
499+ }
500+ return {};
472501 }
473502
474503 DBusResult CreateItemPrompt::updateItem ()
@@ -493,6 +522,9 @@ namespace FdoSecrets
493522 if (ret.err ()) {
494523 return ret;
495524 }
525+
526+ // finally can finish the prompt without dismissing it
527+ finishPrompt (false );
496528 return {};
497529 }
498530} // namespace FdoSecrets
0 commit comments