1 /*
2  *  "GEDKeeper", the personal genealogical database editor.
3  *  Copyright (C) 2009-2021 by Sergey V. Zhdanovskih.
4  *
5  *  This file is part of "GEDKeeper".
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 using System;
22 using System.IO;
23 using BSLib;
24 using BSLib.Design.Graphics;
25 using GDModel;
26 using GKCore.MVP;
27 using GKCore.MVP.Controls;
28 using GKCore.MVP.Views;
29 using GKCore.Types;
30 
31 namespace GKCore.Controllers
32 {
33     /// <summary>
34     ///
35     /// </summary>
36     public sealed class MediaViewerController : DialogController<IMediaViewerWin>
37     {
38         private GDMFileReferenceWithTitle fFileRef;
39         private GDMMultimediaRecord fMultimedia;
40 
41         public GDMFileReferenceWithTitle FileRef
42         {
43             get { return fFileRef; }
44             set {
45                 if (fFileRef != value) {
46                     fFileRef = value;
47                     UpdateView();
48                 }
49             }
50         }
51 
52         public GDMMultimediaRecord Multimedia
53         {
54             get { return fMultimedia; }
55             set { fMultimedia = value; }
56         }
57 
MediaViewerController(IMediaViewerWin view)58         public MediaViewerController(IMediaViewerWin view) : base(view)
59         {
60 
61         }
62 
UpdateView()63         public override void UpdateView()
64         {
65             fView.Title = fFileRef.Title;
66 
67             MultimediaKind mmKind = GKUtils.GetMultimediaKind(fFileRef.MultimediaFormat);
68 
69             try {
70                 switch (mmKind) {
71                     case MultimediaKind.mkImage:
72                         {
73                             IImage img = fBase.Context.LoadMediaImage(fFileRef, false);
74                             if (img != null) {
75                                 fView.SetViewImage(img, fFileRef);
76                             }
77                             break;
78                         }
79 
80                     case MultimediaKind.mkAudio:
81                     case MultimediaKind.mkVideo:
82                         {
83                             string targetFile = fBase.Context.MediaLoad(fFileRef);
84                             fView.SetViewMedia(targetFile);
85                             break;
86                         }
87 
88                     case MultimediaKind.mkText:
89                         {
90                             Stream fs = fBase.Context.MediaLoad(fFileRef, false);
91                             bool disposeStream = (fs != null);
92 
93                             switch (fFileRef.MultimediaFormat) {
94                                 case GDMMultimediaFormat.mfTXT:
95                                     using (StreamReader strd = new StreamReader(fs)) {
96                                         string text = strd.ReadToEnd();
97                                         fView.SetViewText(text);
98                                     }
99                                     break;
100 
101                                 case GDMMultimediaFormat.mfRTF:
102                                     using (StreamReader strd = new StreamReader(fs)) {
103                                         string text = strd.ReadToEnd();
104                                         fView.SetViewRTF(text);
105                                     }
106                                     break;
107 
108                                 case GDMMultimediaFormat.mfHTM:
109                                     disposeStream = false;
110                                     fView.SetViewHTML(fs);
111                                     break;
112                             }
113                             if (disposeStream) fs.Dispose();
114                             break;
115                         }
116                 }
117             } catch (Exception ex) {
118                 fView.DisposeViewControl();
119                 Logger.WriteError("MediaViewerController.UpdateView()", ex);
120             }
121         }
122 
ProcessPortraits(IImageView imageCtl, GDMFileReferenceWithTitle fileRef)123         public void ProcessPortraits(IImageView imageCtl, GDMFileReferenceWithTitle fileRef)
124         {
125             var portraits = GKUtils.SearchPortraits(fBase.Context.Tree, fMultimedia);
126 
127             bool showRegions = (portraits.Count > 0);
128             if (showRegions) {
129                 for (int i = 0; i < portraits.Count; i++) {
130                     imageCtl.AddNamedRegion(portraits[i], (ExtRect)portraits.GetObject(i));
131                 }
132             }
133             imageCtl.ShowNamedRegionTips = showRegions;
134         }
135     }
136 }
137