Extract selected().
[dcpomatic.git] / src / tools / dcpomatic_playlist.cc
index 05dea1f330bfa09c88862ec43ccefd3706d02d53..835582c091d88ceef49f547c8f8a119af99ed8af 100644 (file)
@@ -226,10 +226,20 @@ private:
                _list->SetItemState (_list->GetItemCount() - 1, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
        }
 
-       void delete_playlist ()
+       boost::optional<int> selected() const
        {
-               long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
+               long int selected = _list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
                if (selected < 0 || selected >= int(_playlists.size())) {
+                       return {};
+               }
+
+               return selected;
+       }
+
+       void delete_playlist ()
+       {
+               auto index = selected();
+               if (!index) {
                        return;
                }
 
@@ -238,11 +248,11 @@ private:
                        return;
                }
 
-               boost::filesystem::remove (*dir / (_playlists[selected]->id() + ".xml"));
-               _list->DeleteItem (selected);
-               _playlists.erase (_playlists.begin() + selected);
+               boost::filesystem::remove(*dir / (_playlists[*index]->id() + ".xml"));
+               _list->DeleteItem(*index);
+               _playlists.erase(_playlists.begin() + *index);
 
-               Edit (shared_ptr<SignalSPL>());
+               Edit(shared_ptr<SignalSPL>());
        }
 
        void selection_changed ()