public CitationCluster(CitationItem ci)
 {
     cluster_id     = GetRandomClusterId();
     citation_items = new List <CitationItem>();
     citation_items.Add(ci);
     rtf_hash = null;
 }
        public CitationCluster(string source)
        {
            // Citation cluster is of the form
            //    [.wWw.rtf_hash.wWw.]QIQQA_CLUSTER.oOo.<cluster_id>[.oOo.<reference_key>.oOo.<reference_library_hint>[.xXx.param_key.xXx.param_value]*]*.oOo.

            // Split into the root and the hash
            string rtf_hash = null;

            string[] hash_items = source.Split(SEPARATOR_HASH_ARRAY, StringSplitOptions.None);
            if (1 == hash_items.Length)
            {
                rtf_hash = null;
                source   = hash_items[0];
            }
            else
            {
                rtf_hash = hash_items[1];
                source   = hash_items[2];
            }

            string[] source_items = source.Split(SEPARATOR_CITATION_ARRAY, StringSplitOptions.None);

            // Sanity check
            if (QIQQA_CLUSTER != source_items[0])
            {
                throw new Exception(String.Format("Cannot parse invalid citation cluster: {0}", source));
            }

            // Build the cluster
            cluster_id     = source_items[1];
            citation_items = new List <CitationItem>();
            this.rtf_hash  = rtf_hash;

            // Build the sub_items
            for (int i = 2; i < source_items.Length; i += 2)
            {
                if (source_items[i].Trim().StartsWith(@"\*"))
                {
                    break;
                }

                string   reference_key                 = source_items[i + 0];
                string   remainder                     = source_items[i + 1];
                string[] remainder_split               = remainder.Split(SEPARATOR_PARAMETER_ARRAY, StringSplitOptions.None);
                string   reference_library_hint        = remainder_split[0];
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                for (int j = 1; j < remainder_split.Length; j += 2)
                {
                    parameters[remainder_split[j + 0]] = remainder_split[j + 1];
                }

                CitationItem ci = new CitationItem(reference_key, reference_library_hint, parameters);
                citation_items.Add(ci);
            }
        }
        public static CitationCluster GenerateCitationClusterFromPDFDocuments(List <PDFDocument> selected_pdf_documents)
        {
            foreach (PDFDocument pdf_document in selected_pdf_documents)
            {
                pdf_document.DateLastCited = DateTime.UtcNow;
            }

            // Check if any of these documents have duplicated BibTeX keys...
            foreach (PDFDocument pdf_document in selected_pdf_documents)
            {
                string key = pdf_document.BibTexKey;
                if (!String.IsNullOrEmpty(key))
                {
                    foreach (PDFDocument pdf_document_other in pdf_document.Library.PDFDocuments)
                    {
                        if (pdf_document_other != pdf_document)
                        {
                            if (!String.IsNullOrEmpty(pdf_document_other.BibTex) && pdf_document_other.BibTex.Contains(key))
                            {
                                if (pdf_document_other.BibTexKey == key)
                                {
                                    MessageBoxes.Warn(String.Format("There are several document in your library with the same BibTeX key '{0}'.  Unless they are the same PDF, you should give them different keys or else InCite will just pick the first matching document.", key));
                                }
                            }
                        }
                    }
                }
            }

            // Check if any of these documents have no BibTeX key...
            foreach (PDFDocument pdf_document in selected_pdf_documents)
            {
                string key = pdf_document.BibTexKey;
                if (String.IsNullOrEmpty(key))
                {
                    MessageBoxes.Warn(String.Format("Some of your documents (e.g. with title, '{0}') have no BibTeX key.  Without a unique BibTeX key, Qiqqa has no way of referencing this document from Word, and they will show up in Word as either [ ] or a blank when you wave the magic wand.  Please add any missing keys and try again!", pdf_document.TitleCombined));
                }
            }

            if (0 < selected_pdf_documents.Count)
            {
                CitationCluster cc = new CitationCluster();
                foreach (PDFDocument selected_pdf_document in selected_pdf_documents)
                {
                    string reference_key = selected_pdf_document.BibTexKey;
                    if (null != reference_key)
                    {
                        string       reference_library = selected_pdf_document.Library.WebLibraryDetail.Id;
                        CitationItem ci = new CitationItem(reference_key, reference_library);
                        cc.citation_items.Add(ci);
                    }
                    else
                    {
                        MessageBoxes.Warn("Could not add document '{0}' because it doesn't have valid BibTeX or a valid BibTeX key.", selected_pdf_document.TitleCombined);
                    }
                }

                return(cc);
            }

            MessageBoxes.Warn("You have not selected any document(s) to cite.");
            return(null);
        }
        private void ReflectCitationCluster(CitationCluster current_citation_cluster)
        {
            if (null == current_citation_cluster)
            {
                this.IsEnabled = false;
                this.ObjGridNoCitationSelectedInstructions.Visibility = Visibility.Visible;
                this.ObjGridNoCitationSelectedInstructions.Background = ThemeColours.Background_Brush_Blue_Dark;
                //this.ObjGridCitationSelectedPanel.Visibility = Visibility.Collapsed;

                ObjCitationsInCluster.ItemsSource    = null;
                ObjCheckSeparateAuthorDate.IsChecked = false;
                ObjSpecifierType.Text     = "";
                ObjSpecifierLocation.Text = "";
                ObjPrefix.Text            = "";
                ObjSuffix.Text            = "";
            }
            else
            {
                this.IsEnabled = true;
                this.ObjGridNoCitationSelectedInstructions.Visibility = Visibility.Collapsed;
                //this.ObjGridCitationSelectedPanel.Visibility = Visibility.Visible;

                // Populate the list of items
                ObservableCollection <string> citation_item_keys = new ObservableCollection <string>();
                foreach (var citation_item in current_citation_cluster.citation_items)
                {
                    citation_item_keys.Add(citation_item.reference_key);
                }
                ObjCitationsInCluster.ItemsSource = citation_item_keys;

                // Populate the options - we only use the first item's stuff...
                if (0 < current_citation_cluster.citation_items.Count)
                {
                    CitationItem citation_item = current_citation_cluster.citation_items[0];

                    // Separate author date?
                    string separate_author_date = citation_item.GetParameter(CitationItem.PARAM_SEPARATE_AUTHOR_DATE);
                    ObjCheckSeparateAuthorDate.IsChecked = (CitationItem.OPTION_SEPARATE_AUTHOR_DATE_TRUE == separate_author_date);

                    // Locator type?
                    string specifier_type = citation_item.GetParameter(CitationItem.PARAM_SPECIFIER_TYPE);
                    ObjSpecifierType.Text = specifier_type;

                    // For the actual specifier, lets allow multiple selections
                    string specifier_locations = "";
                    string prefixes            = "";
                    string suffixes            = "";
                    for (int i = 0; i < current_citation_cluster.citation_items.Count; ++i)
                    {
                        specifier_locations = specifier_locations + current_citation_cluster.citation_items[i].GetParameter(CitationItem.PARAM_SPECIFIER_LOCATION) + ";";
                        prefixes            = prefixes + current_citation_cluster.citation_items[i].GetParameter(CitationItem.PARAM_PREFIX) + ";";
                        suffixes            = suffixes + current_citation_cluster.citation_items[i].GetParameter(CitationItem.PARAM_SUFFIX) + ";";
                    }
                    specifier_locations       = specifier_locations.TrimEnd(';');
                    prefixes                  = prefixes.TrimEnd(';');
                    suffixes                  = suffixes.TrimEnd(';');
                    ObjSpecifierLocation.Text = specifier_locations;
                    ObjPrefix.Text            = prefixes;
                    ObjSuffix.Text            = suffixes;
                }
            }
        }