public void AddPlaylist(dsLanguageData.CardDataTable cards, string description)
        {
            List<int> cardIDs = new List<int>();
            foreach (dsLanguageData.CardRow card in cards)
            {
                cardIDs.Add(card.ID);
            }

            AddPlaylist(cardIDs, description);
        }
        void loadResults(dsLanguageData.CardDataTable cards)
        {
            listViewResults.Items.Clear();

            if (cards.Rows.Count < 1)
                listViewResults.Items.Add("No matches found");
            foreach (dsLanguageData.CardRow card in cards)
            {
                listViewResults.Items.Add(new IDListViewItem(card.ID, string.Format("{0} -- {1}", card.Answer, card.Question)));
            }
        }
        public int InsertOrUpdateDictionary(dsLanguageData.DictionaryRow dictionary)
        {
            int i = 0;

            dsLanguageData.DictionaryDataTable dtDict = daDictionary.GetDataByID(dictionary.ID);
            if (dtDict.Rows.Count < 1 || dictionary.ID < 0)
            {
                dictionary.ID = getNewIdForTable(new dsLanguageData.DictionaryDataTable().TableName);
                i = daDictionary.Insert(dictionary.ID, dictionary.Name, dictionary.Alphabet, dictionary.Column);
            }
            else
            {
                dsLanguageData.DictionaryRow d = dtDict[0];
                if (d.ID != dictionary.ID || d.Name != dictionary.Name || d.Alphabet != dictionary.Alphabet || d.Column != dictionary.Column)
                {
                    i = daDictionary.UpdateDictionary(dictionary.ID, dictionary.Name, dictionary.Alphabet, dictionary.Column);
                }
            }

            return i;
        }
        public int InsertCollection(dsLanguageData.CollectionRow collection)
        {
            collection.ID = getNewIdForTable(new dsLanguageData.CollectionDataTable().TableName);
            int i = daCollection.Insert(collection.ID, collection.Name, DateTime.Now, DateTime.Now, collection.CanEdit);

            return i;
        }
        public int ImportFromOldDB(string oldDBConString, bool importMarks)
        {
            int affectedRows = 0;
            LangLearner.Data.LLSqlite oldSqlite = new LangLearner.Data.LLSqlite(oldDBConString);
            LangLearner.Data.dsLanguageDB.CardListDataTable dtOldCardList = oldSqlite.GetCardLists();
            dsLanguageData dsNewData = new dsLanguageData();
            int defaultCollectionID = getOrCreateDefaultImportCollectionID();

            //Go through each old card list and add any missing cards
            int progressIndex = 0;
            foreach (LangLearner.Data.dsLanguageDB.CardListRow oldCardListRow in dtOldCardList)
            {
                progressIndex++;

                //First if the Card list is not in the new DB. Add it.
                if (daCardList.GetDataByName(oldCardListRow.Name.Trim()).Rows.Count < 1)
                {
                    affectedRows = daCardList.Insert(getNewIdForTable(dsNewData.CardList.TableName), defaultCollectionID, oldCardListRow.Name, DateTime.Now, DateTime.Now);

                    if (affectedRows < 1)
                    {
                        DataImported(new DataImportedEventArgs(false, string.Format("Could not create CardList: {0}", oldCardListRow.Name), progressIndex, dtOldCardList.Rows.Count));
                        continue;
                    }
                    else
                    {
                        DataImported(new DataImportedEventArgs(true, string.Format("Added CardList: {0}", oldCardListRow.Name), progressIndex, dtOldCardList.Rows.Count));
                    }
                }
                else
                {
                    DataImported(new DataImportedEventArgs(true, string.Format("CardList already exists: {0}. Checking for new cards...", oldCardListRow.Name), progressIndex, dtOldCardList.Rows.Count));
                }

                int cardListID = (int)daCardList.ScalarQueryGetIDByName(oldCardListRow.Name.Trim());

                //Get the old cards that were in this playlist
                List<string> oldCardListGuid = new List<string>();
                oldCardListGuid.Add(oldCardListRow.Guid);
                LangLearner.Data.dsLanguageDB.CardDataTable dtOldCards = oldSqlite.GetCardsInLists(oldCardListGuid);

                //Now import the cards
                foreach (LangLearner.Data.dsLanguageDB.CardRow oldCardRow in dtOldCards)
                {
                    //Insert new cards if they don't exists
                    dsLanguageData.CardDataTable dtNewCard = daCard.GetDataByCardInfo(oldCardRow.Question, oldCardRow.Answer);
                    if (dtNewCard.Rows.Count < 1)
                    {
                        bool markForReview = false;
                        int difficulty = 1;

                        if (importMarks)
                        {
                            markForReview = oldCardRow.IsMarkedForReview;
                            if (oldCardRow.IsEasy)
                                difficulty = 0;
                        }

                        affectedRows += daCard.Insert(getNewIdForTable(new dsLanguageData.CardDataTable().TableName), oldCardRow.Question, oldCardRow.Answer, oldCardRow.Example, oldCardRow.Count, oldCardRow.DateCreated, DateTime.Now, markForReview, difficulty);
                        //TODO error handling
                    }

                    int cardID = daCard.GetDataByCardInfo(oldCardRow.Question, oldCardRow.Answer)[0].ID;

                    //Check for and import the associated SoundClip
                    if (oldCardRow.SoundFileGuid != null && oldCardRow.SoundFileGuid != Guid.Empty.ToString())
                    {
                        dsLanguageData.SoundClipDataTable dtSoundClip = daSoundClip.GetDataByCardID(cardID);
                        if (dtSoundClip.Rows.Count < 1)
                        {
                            //Convert the old sound row in put in the new one
                            LangLearner.Data.dsLanguageDB.SoundFileRow oldSoundRow = oldSqlite.GetSoundFile(oldCardRow.SoundFileGuid);
                            MemoryStream clipStream = streamFromString(oldSoundRow.SoundFile);

                            affectedRows += daSoundClip.Insert(getNewIdForTable(new dsLanguageData.SoundClipDataTable().TableName), oldSoundRow.Name, clipStream.ToArray(), oldSoundRow.DateCreated, DateTime.Now, cardID);
                            //TODO error handling
                        }
                    }

                    //Make sure the card is added to the current playlist
                    dsLanguageData.CardListDataDataTable dtCardListData = daCardListData.GetDataByIDs(cardListID, cardID);
                    if (dtCardListData.Rows.Count < 1)
                    {
                        //TODO make CardListData insert, edit position methods
                        affectedRows += daCardListData.Insert(getNewIdForTable(new dsLanguageData.CardListDataDataTable().TableName), cardListID, cardID, getNewCardListDataIndex(cardListID));
                        //TODO error handling
                    }
                }
            }

            return affectedRows;
        }
        void loadCard(dsLanguageData.CardRow card)
        {
            clearCard();

            _curCard = card;

            if (_curCard != null)
            {
                txtAnswer.Text = _curCard.Answer;
                txtQuestion.Text = _curCard.Question;

                LanguageData dataLayer = new LanguageData();
                dsLanguageData.PictureDataTable dtPic = dataLayer.daPicture.GetDataByCardID(_curCard.ID);
                if (dtPic != null && dtPic.Rows.Count > 0 && dtPic[0].Image.Length > 0)
                {
                    cardPreviewPictureBox.Image = dataLayer.ByteArrayToImage(dtPic[0].Image);
                }
            }
        }
 public CardChangedEventArgs(dsLanguageData.CardRow card, dsLanguageData.SoundClipRow sound)
 {
     Card = card;
     SoundClip = sound;
 }
        dsLanguageData.CardRow writeDataToCard(dsLanguageData.CardRow card)
        {
            if (card != null)
            {
                card.LastModified = DateTime.Now;
                card.Notes = txtNotes.Text;
                card.Difficulty = Difficulty;

                ShowInformation();
                if (chkBoxSwitchQuestionAnswer.Checked)
                {
                    card.Question = txtAnswer.Text;
                    card.Answer = txtQuestion.Text;
                }
                else
                {
                    card.Answer = txtAnswer.Text;
                    card.Question = txtQuestion.Text;
                }
            }

            return card;
        }
 public static dsLanguageData.CollectionRow CloneRow(dsLanguageData.CollectionRow row)
 {
     dsLanguageData.CollectionRow newRow = new CollectionDataTable().NewCollectionRow();
     newRow.ItemArray = row.ItemArray;
     return newRow;
 }
 public static dsLanguageData.CardListRow CloneRow(dsLanguageData.CardListRow row)
 {
     dsLanguageData.CardListRow newRow = new CardListDataTable().NewCardListRow();
     newRow.ItemArray = row.ItemArray;
     return newRow;
 }
 public void CloneAndAddRow(dsLanguageData.CardRow card)
 {
     this.Rows.Add(CardDataTable.CloneRow(card, this));
 }
 public static dsLanguageData.SoundClipRow CloneRow(dsLanguageData.SoundClipRow row)
 {
     dsLanguageData.SoundClipRow newRow = new SoundClipDataTable().NewSoundClipRow();
     newRow.ItemArray = row.ItemArray;
     return newRow;
 }
 public static dsLanguageData.CardRow CloneRow(dsLanguageData.CardRow card, CardDataTable dtCard)
 {
     dsLanguageData.CardRow newRow = dtCard.MakeNewCardRow();
     newRow.ItemArray = card.ItemArray;
     return newRow;
 }
 public static dsLanguageData.PictureRow CloneRow(dsLanguageData.PictureRow row)
 {
     dsLanguageData.PictureRow newRow = new PictureDataTable().NewPictureRow();
     newRow.ItemArray = row.ItemArray;
     return newRow;
 }
 public static dsLanguageData.DictionaryRow CloneRow(dsLanguageData.DictionaryRow row)
 {
     dsLanguageData.DictionaryRow newRow = new DictionaryDataTable().NewDictionaryRow();
     newRow.ItemArray = row.ItemArray;
     return newRow;
 }
 /// <summary>
 /// Gets the text from a card that should be shown in the question box.
 /// </summary>
 /// <param name="card"></param>
 /// <returns></returns>
 string getTextForQuestion(dsLanguageData.CardRow card)
 {
     if (chkBoxSwitchQuestionAnswer.Checked)
         return card.Answer;
     return card.Question;
 }
        bool isCardEmpty(dsLanguageData.CardRow card, dsLanguageData.SoundClipRow soundClip)
        {
            bool empty = true;

            if (card == null)
                return empty;

            if (card.Answer.Trim().Length > 0)
                empty = false;
            if (card.Question.Trim().Length > 0)
                empty = false;
            if (card.Notes.Trim().Length > 0)
                empty = false;
            if (soundClip != null && soundClip.SoundClip.Length > 0)
                empty = false;

            return empty;
        }
        /// <summary>
        /// Adds a card to the current list. If no list is selected then a new custom list is created.
        /// </summary>
        /// <param name="card"></param>
        public void AddCard(dsLanguageData.CardRow card)
        {
            Cards.CloneAndAddRow(card);

            if (Cards.Rows.Count < 2)
            {
                Description = "Custom Playlist";
                onPlayListChanged(PlaylistAction.Loaded);
            }
            else
            {
                onPlayListChanged(PlaylistAction.Changed);
            }
        }
        dsLanguageData.SoundClipRow writeSoundDataToSoundClip(dsLanguageData.SoundClipRow sound)
        {
            if (sound != null)
            {
                sound.CardID = _curCard.ID;
                sound.Name = getAnswerText();
            }

            return sound;
        }
        public void AddOrLoadCards(dsLanguageData.CardDataTable cards, string description, List<int> cardListIDs, AddOrLoad action)
        {
            if (action == AddOrLoad.Load)
            {
                ClearCards();
            }

            if (cards.Rows.Count < 1 && description.Trim().Length < 1)
                return;

            btnHeading.Text = string.Format("Playlist: {0}", description);

            CardListIDs = cardListIDs;
            bool wasEmpty = Cards.Count < 1;
            Cards.Merge(cards);
            RefreshCards(false);

            //deciede whether or not to make a new list or just add to the old one
            if (action == AddOrLoad.Load || wasEmpty )
            {
                Description = description;
                onPlayListChanged(PlaylistAction.Loaded);
                SelectRandomCard();
            }
            else if (action == AddOrLoad.Add)
            {
                onPlayListChanged(PlaylistAction.Changed);
            }
        }
        bool isValidRow(dsLanguageData.DictionaryRow row)
        {
            bool isValid = true;

            if (row.ID < 0)
                isValid = false;
            if (row.Name == null || row.Name.Trim().Length < 1)
                isValid = false;
            if (row.Alphabet == null || row.Alphabet.Trim().Length < 1)
                isValid = false;
            if (row.Column == null || row.Column.Trim().Length < 1)
                isValid = false;

            return isValid;
        }
        /// <summary>
        /// Updates a card that has been changed
        /// </summary>
        /// <param name="card"></param>
        public void UpdateCard(dsLanguageData.CardRow card)
        {
            for (int i = 0; i < Cards.Count; i++)
            {
                if (Cards[i].ID == card.ID)
                    ((dsLanguageData.CardRow)Cards.Rows[i]).ItemArray = card.ItemArray;
            }

            RefreshCards(false);
        }
        string getDisplayText(dsLanguageData.CardRow row)
        {
            string text = string.Empty;

            if (_switchQuestionAndAnswerText)
            {
                text = row.Answer;
                if (text.Trim().Length < 1)
                    text = row.Question;
            }
            else
            {
                text = row.Question;
                if (text.Trim().Length < 1)
                    text = row.Answer;
            }
            return text;
        }
 public CardSelectedEventArgs(dsLanguageData.CardRow card)
 {
     Card = card;
     CardWasSelected = true;
 }
        public int InsertCardList(dsLanguageData.CardListRow cardList)
        {
            cardList.ID = getNewIdForTable(new dsLanguageData.CardListDataTable().TableName);
            int i = daCardList.Insert(cardList.ID, cardList.CollectionID, cardList.Name, DateTime.Now, DateTime.Now);

            return i;
        }
        /// <summary>
        /// Loads the card and also checks to make sure the user is finished with the previous card.
        /// </summary>
        /// <param name="card"></param>
        /// <returns>Whether or not the card was loaded</returns>
        public bool LoadCard(dsLanguageData.CardRow card)
        {
            bool result = false;

            if (ChangesHaveBeenSavedOrIgnored())
            {
                ClearCard();

                if (card == null)
                    return true;

                SuspendLayout();

                _curCard = card;

                LanguageData dataLayer = new LanguageData();

                dsLanguageData.SoundClipDataTable dtSound = dataLayer.daSoundClip.GetDataByCardID(_curCard.ID);
                if (dtSound.Rows.Count > 0 && dtSound[0].SoundClip != null)
                {
                    _curSoundClip = dtSound[0];
                    _dictaphone.WavStream = new MemoryStream(_curSoundClip.SoundClip);
                    _dictaphone.ClosePlayer();
                    setSoundButton(true);
                }

                dsLanguageData.PictureDataTable dtPic = dataLayer.daPicture.GetDataByCardID(_curCard.ID);
                if (dtPic.Rows.Count > 0 && dtPic[0].Image != null)
                {
                    pictureBox.Image = dataLayer.ByteArrayToImage(dtPic[0].Image);
                }

                txtQuestion.Text = getTextForQuestion(_curCard);

                if (chkBoxShowAnswer.Checked)
                    ShowInformation();

                txtNotes.Text = _curCard.Notes;
                Difficulty = _curCard.Difficulty;

                result = true;

                ResumeLayout();
            }

            if (result)
            {
                SetEnable(true);
            }

            return result;
        }
        public int InsertOrUpdateCard(dsLanguageData.CardRow card)
        {
            int i = 0;

            if (daCard.GetDataByCardID(card.ID).Rows.Count < 1)
            {
                if (card.ID < 1)
                    card.ID = getNewIdForTable(new dsLanguageData.CardDataTable().TableName);

                i = daCard.Insert(card.ID, card.Question, card.Answer, card.Notes, card.Count, DateTime.Now, DateTime.Now, card.MarkForReview, card.Difficulty);
            }
            else
            {
                card.LastModified = DateTime.Now;
                i = daCard.UpdateCard(card.ID, card.Question, card.Answer, card.Notes, card.Count, card.DateCreated, DateTime.Now, card.MarkForReview, card.Difficulty, card.ID);
            }

            return i;
        }
 void deleteCard(dsLanguageData.CardRow card)
 {
     LanguageData dataLayer = new LanguageData();
     dataLayer.DeleteCard(card.ID);
     ClearCard();
 }
        public int InsertUpdateOrDeleteSoundClip(dsLanguageData.SoundClipRow sound)
        {
            int i = 0;

            if (sound != null)
            {
                //if its being deleted
                if (sound.SoundClip.Length == 0 || sound.RowState == DataRowState.Deleted)
                {
                    i = daSoundClip.DeleteSoundClip(sound.ID);
                }
                else
                {
                    if (sound.CardID < 1)
                    {
                        throw new MissingFieldException("SoundClip is missing its corresponding CardID");
                    }

                    if (daSoundClip.GetDataByID(sound.ID).Rows.Count < 1)
                    {
                        if (sound.ID < 1)
                            sound.ID = getNewIdForTable(new dsLanguageData.SoundClipDataTable().TableName);

                        i = daSoundClip.Insert(sound.ID, sound.Name, sound.SoundClip, DateTime.Now, DateTime.Now, sound.CardID);
                    }
                    else
                    {
                        sound.LastModified = DateTime.Now;
                        i = daSoundClip.Update(sound);
                    }
                }
            }

            return i;
        }
        /// <summary>
        /// Loads all of the cards images in the results panel
        /// </summary>
        /// <param name="cards"></param>
        public void LoadImagesForCards(dsLanguageData.CardDataTable cards)
        {
            resultsFlowLayoutPanel.Controls.Clear();

            LanguageData dataLayer = new LanguageData();

            resultsFlowLayoutPanel.SuspendLayout();

            foreach (dsLanguageData.CardRow card in cards)
            {
                Image img = dataLayer.GetPictureForCard(card.ID);
                AddImagePreview(img, card.Answer, true, true);
            }

            resultsFlowLayoutPanel.ResumeLayout();
        }