Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new media object.
        /// </summary>
        /// <param name="media">The memory stream containing the media.</param>
        /// <param name="type">The media type.</param>
        /// <param name="rpu">A delegate of type <see cref="StatusMessageReportProgress"/> used to send messages back to the calling object.</param>
        /// <param name="caller">The calling object.</param>
        /// <returns>The id for the new media object.</returns>
        /// <remarks>Documented by Dev03, 2008-08-05</remarks>
        /// <remarks>Documented by Dev03, 2009-01-13</remarks>
        public int CreateMedia(Stream media, EMedia type, StatusMessageReportProgress rpu, object caller)
        {
            using (NpgsqlConnection conn = PostgreSQLConn.CreateConnection(Parent.CurrentUser))
            {
                NpgsqlTransaction tran = conn.BeginTransaction();

                LargeObjectManager lbm  = new LargeObjectManager(conn);
                int         noid        = lbm.Create(LargeObjectManager.READWRITE);
                LargeObject largeObject = lbm.Open(noid, LargeObjectManager.READWRITE);
                byte[]      buffer      = new byte[media.Length];
                media.Read(buffer, 0, (int)media.Length);
                BufferToLargeObject(buffer, largeObject, rpu, caller);
                largeObject.Close();

                int newId = 0;
                using (NpgsqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "INSERT INTO \"MediaContent\" (data, media_type) VALUES (:data, :type) RETURNING id;";
                    cmd.Parameters.Add("data", noid);
                    cmd.Parameters.Add("type", type.ToString());
                    newId = Convert.ToInt32(PostgreSQLConn.ExecuteScalar(cmd, Parent.CurrentUser));
                }
                tran.Commit();

                return(newId);
            }
        }
Ejemplo n.º 2
0
 protected XmlMedia(XmlDictionary dictionary, EMedia mediaType, string filename, ParentClass parent)
 {
     this.parent       = parent;
     m_mediaIdentifier = mediaType;
     m_oDictionary     = dictionary;
     m_filename        = filename;
 }
Ejemplo n.º 3
0
 protected XmlMedia(XmlDictionary dictionary, EMedia mediaType, string filename, ParentClass parent)
 {
     this.parent = parent;
     m_mediaIdentifier = mediaType;
     m_oDictionary = dictionary;
     m_filename = filename;
 }
Ejemplo n.º 4
0
 public PreviewMedia(EMedia type, string path, bool isActive, bool isDefault, bool isExample, ParentClass parent)
 {
     this.parent    = parent;
     this.mediatype = type;
     this.filename  = path;
     this.active    = isActive;
     this._default  = isDefault;
     this.example   = isExample;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Copies the media to.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <remarks>Documented by Dev02, 2009-06-25</remarks>
        private void CopyMediaTo(DbCardStyle target)
        {
            //mapping dictionary current id, new id
            Dictionary <int, int> mediaMapping = new Dictionary <int, int>();

            //copy media objects
            foreach (int mediaId in connector.GetMediaForCardStyle(Id))
            {
                int newMediaId;

                //fetch current media data
                using (Stream mediaStream = mediaConnector.GetMediaStream(mediaId))
                {
                    EMedia mediaType = mediaConnector.GetMediaType(mediaId);
                    Dictionary <MediaProperty, string> mediaProperties = mediaConnector.GetProperties(mediaId);

                    //copy to new
                    newMediaId = target.mediaConnector.CreateMedia(mediaStream, mediaType, delegate { return(false); }, null);
                    target.mediaConnector.SetProperties(newMediaId, mediaProperties);
                }

                //add
                target.connector.AddMediaForCardStyle(target.Id, newMediaId);
                mediaMapping[mediaId] = newMediaId;
            }

            //translate ids
            ITextStyle[] styles = { target.xmlStyle.Question, target.xmlStyle.QuestionExample,
                                    target.xmlStyle.Answer,   target.xmlStyle.AnswerExample,  target.xmlStyle.AnswerCorrect, target.xmlStyle.AnswerWrong };
            foreach (ITextStyle style in styles)
            {
                string[] keys = new String[style.OtherElements.Keys.Count];
                style.OtherElements.Keys.CopyTo(keys, 0);
                foreach (string key in keys)
                {
                    style.OtherElements[key] = cssUrlEntry.Replace(style.OtherElements[key], delegate(Match urlMatch)
                    {
                        return(replaceMediaIds.Replace(urlMatch.Groups[1].Value, delegate(Match mediaIdMatch)
                        {
                            int id = Convert.ToInt32(mediaIdMatch.Groups[1].Value);
                            if (mediaMapping.ContainsKey(id))
                            {
                                return string.Format(mediaPlaceHolder, mediaMapping[id]);
                            }
                            return urlMatch.Groups[0].Value;
                        }));
                    });
                }
            }
        }
Ejemplo n.º 6
0
        internal DbMedia(int id, int cardid, bool checkId, EMedia mediaIdentifier, Side side, WordType type, bool isDefault, bool isExample, ParentClass parent)
        {
            this.parent = parent;

            if (checkId)
                connector.CheckCardMediaId(id, cardid);

            this.id = id;
            this.cardid = cardid;
            this.mediaIdentifier = mediaIdentifier;
            this.side = side;
            this.type = type;
            this.isDefault = isDefault;
            this.isExample = isExample;
        }
Ejemplo n.º 7
0
        public void ClearCardMedia(int cardid, Side side, WordType type, EMedia mediatype)
        {
            using (SqlCeCommand cmd = MSSQLCEConn.CreateCommand(Parent.CurrentUser))
            {
                cmd.CommandText = "DELETE FROM \"Cards_MediaContent\" WHERE cards_id=@cardid AND side=@side AND type=@type " +
                                  "AND media_id IN (SELECT id FROM \"MediaContent\" WHERE media_type=@mediatype);";
                cmd.Parameters.Add("@cardid", cardid);
                cmd.Parameters.Add("@side", side.ToString());
                cmd.Parameters.Add("@type", type.ToString());
                cmd.Parameters.Add("@mediatype", mediatype.ToString());
                cmd.ExecuteNonQuery();
            }

            Parent.CurrentUser.Cache.Uncache(ObjectLifetimeIdentifier.GetIdentifier(CacheObject.CardMedia, cardid));
        }
Ejemplo n.º 8
0
        internal DbMedia(int id, int cardid, bool checkId, EMedia mediaIdentifier, Side side, WordType type, bool isDefault, bool isExample, ParentClass parent)
        {
            this.parent = parent;

            if (checkId)
            {
                connector.CheckCardMediaId(id, cardid);
            }

            this.id              = id;
            this.cardid          = cardid;
            this.mediaIdentifier = mediaIdentifier;
            this.side            = side;
            this.type            = type;
            this.isDefault       = isDefault;
            this.isExample       = isExample;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Creates a disconnected card media object.
        /// </summary>
        /// <param name="id">The id of the media.</param>
        /// <param name="mediaIdentifier">The media type identifier.</param>
        /// <param name="isDefault">if set to <c>true</c> [is default].</param>
        /// <param name="isExample">if set to <c>true</c> [is example].</param>
        /// <param name="parent">The parent.</param>
        /// <returns></returns>
        /// <remarks>
        /// Documented by AAB, 6.8.2008.
        /// </remarks>
        internal static IMedia CreateDisconnectedCardMedia(int id, EMedia mediaIdentifier, bool isDefault, bool isExample, ParentClass parent)
        {
            switch (mediaIdentifier)
            {
            case EMedia.Audio:
                return(new DbAudio(id, -1, false, Side.Answer, WordType.Word, isDefault, isExample, parent));

            case EMedia.Image:
                return(new DbImage(id, -1, false, Side.Answer, WordType.Word, isDefault, isExample, parent));

            case EMedia.Video:
                return(new DbVideo(id, -1, false, Side.Answer, WordType.Word, isDefault, isExample, parent));

            default:
                throw new ArgumentException("Invalid EMedia type.");
            }
        }
        public void ClearCardMedia(int cardid, Side side, WordType type, EMedia mediatype)
        {
            using (NpgsqlConnection con = PostgreSQLConn.CreateConnection(Parent.CurrentUser))
            {
                using (NpgsqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = "DELETE FROM \"Cards_MediaContent\" WHERE cards_id=:cardid AND side=:side AND type=:type " +
                                      "AND media_id IN (SELECT id FROM \"MediaContent\" WHERE media_type=:mediatype);";
                    cmd.Parameters.Add("cardid", cardid);
                    cmd.Parameters.Add("side", side.ToString());
                    cmd.Parameters.Add("type", type.ToString());
                    cmd.Parameters.Add("mediatype", mediatype.ToString());
                    cmd.ExecuteNonQuery();
                }
            }

            Parent.CurrentUser.Cache.Uncache(ObjectLifetimeIdentifier.GetIdentifier(CacheObject.CardMedia, cardid));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Checks if there is a media from the given type in the list
        /// </summary>
        /// <param name="mediaList">The media list.</param>
        /// <param name="mediaType">Type of the media.</param>
        /// <returns></returns>
        /// <remarks>Documented by Dev08, 2009-07-16</remarks>
        private bool ExistsMedia(IList <IMedia> mediaList, EMedia mediaType)
        {
            if (mediaList.Count == 0)
            {
                return(false);
            }

            bool mediaFound = false;

            foreach (IMedia media in mediaList)
            {
                if (media.MediaType == mediaType)
                {
                    mediaFound = true;
                    break;
                }
            }

            return(mediaFound);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Gets the Media.
        /// </summary>
        /// <param name="card">The card.</param>
        /// <param name="side">The side.</param>
        /// <param name="type">The type.</param>
        /// <param name="donotChange">if set to <c>true</c> do not change the direction.</param>
        /// <returns></returns>
        /// <remarks>Documented by Dev05, 2008-08-06</remarks>
        private IMedia GetMedia(ICard card, Side side, EMedia type, bool donotChange)
        {
            if (dictionary.CurrentQueryDirection == EQueryDirection.Answer2Question && !donotChange)
            {
                if (side == Side.Question)
                {
                    side = Side.Answer;
                }
                else
                {
                    side = Side.Question;
                }
            }

            switch (side)
            {
            case Side.Question:
                foreach (IMedia media in card.QuestionMedia)
                {
                    if (media.MediaType == type)
                    {
                        return(media);
                    }
                }
                break;

            case Side.Answer:
                foreach (IMedia media in card.AnswerMedia)
                {
                    if (media.MediaType == type)
                    {
                        return(media);
                    }
                }
                break;
            }

            return(null);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Creates the new media object.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="path">The path.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <param name="isDefault">if set to <c>true</c> [is default].</param>
        /// <param name="isExample">if set to <c>true</c> [is example].</param>
        /// <returns></returns>
        /// <remarks>Documented by Dev03, 2009-03-23</remarks>
        public static IMedia CreateNewMediaObject(EMedia type, string path, bool isActive, bool isDefault, bool isExample)
        {
            IMedia media;

            switch (type)
            {
            case EMedia.Audio:
                media = new PreviewAudio(path, isActive, isDefault, isExample, null);
                break;

            case EMedia.Image:
                media = new PreviewImage(path, isActive, isDefault, isExample, null);
                break;

            case EMedia.Video:
                media = new PreviewVideo(path, isActive, isDefault, isExample, null);
                break;

            default:
                media = new PreviewMedia(type, path, isActive, isDefault, isExample, null);
                break;
            }
            return(media);
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Creates a disconnected card media object.
 /// </summary>
 /// <param name="id">The id of the media.</param>
 /// <param name="mediaIdentifier">The media type identifier.</param>
 /// <param name="isDefault">if set to <c>true</c> [is default].</param>
 /// <param name="isExample">if set to <c>true</c> [is example].</param>
 /// <returns></returns>
 /// <remarks>Documented by Dev03, 2008-08-06</remarks>
 internal IMedia CreateDisconnectedCardMedia(int id, EMedia mediaIdentifier, bool isDefault, bool isExample)
 {
     switch (mediaIdentifier)
     {
         case EMedia.Audio:
             return new DbAudio(id, -1, false, Side.Answer, WordType.Word, isDefault, isExample, parent);
         case EMedia.Image:
             return new DbImage(id, -1, false, Side.Answer, WordType.Word, isDefault, isExample, parent);
         case EMedia.Video:
             return new DbVideo(id, -1, false, Side.Answer, WordType.Word, isDefault, isExample, parent);
         default:
             throw new ArgumentException("Invalid EMedia type.");
     }
 }
Ejemplo n.º 15
0
 protected XmlMedia(EMedia mediaType, string filename, ParentClass parent)
 {
     this.parent = parent;
     m_mediaIdentifier = mediaType;
     m_filename = filename;
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Creates the media object.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="path">The path.</param>
 /// <param name="isActive">if set to <c>true</c> [is active].</param>
 /// <param name="isDefault">if set to <c>true</c> [is default audio].</param>
 /// <param name="isExample">if set to <c>true</c> [is example audio].</param>
 /// <returns>A media object that implements IMedia.</returns>
 /// <remarks>Documented by Dev03, 2007-08-07</remarks>
 public IMedia CreateMedia(EMedia type, string path, bool isActive, bool isDefault, bool isExample)
 {
     return (parent.GetParentDictionary() as XmlDictionary).CreateNewMediaObject(type, path, isActive, isDefault, isExample);
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Adds the Media to the spezified card.
 /// </summary>
 /// <param name="cardID">The card ID.</param>
 /// <param name="filename">The filename.</param>
 /// <param name="type">The type.</param>
 /// <param name="side">The side.</param>
 /// <param name="active">if set to <c>true</c> the file is active.</param>
 /// <param name="standard">if set to <c>true</c> the file is the default file.</param>
 /// <param name="example">if set to <c>true</c> the file is the example.</param>
 /// <remarks>Documented by Dev05, 2007-09-03</remarks>
 public void AddMedia(int cardID, string filename, EMedia type, Side side, bool active, bool standard, bool example)
 {
     ICard card = cards.Get(cardID);
     card.AddMedia(card.CreateMedia(type, filename, active, standard, example), side);
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Gets the Media.
        /// </summary>
        /// <param name="card">The card.</param>
        /// <param name="side">The side.</param>
        /// <param name="type">The type.</param>
        /// <param name="donotChange">if set to <c>true</c> do not change the direction.</param>
        /// <returns></returns>
        /// <remarks>Documented by Dev05, 2008-08-06</remarks>
        private IMedia GetMedia(ICard card, Side side, EMedia type, bool donotChange)
        {
            if (dictionary.CurrentQueryDirection == EQueryDirection.Answer2Question && !donotChange)
            {
                if (side == Side.Question)
                    side = Side.Answer;
                else
                    side = Side.Question;
            }

            switch (side)
            {
                case Side.Question:
                    foreach (IMedia media in card.QuestionMedia)
                    {
                        if (media.MediaType == type)
                            return media;
                    }
                    break;
                case Side.Answer:
                    foreach (IMedia media in card.AnswerMedia)
                    {
                        if (media.MediaType == type)
                            return media;
                    }
                    break;
            }

            return null;
        }
Ejemplo n.º 19
0
 /// <summary>
 /// Creates a new media object.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="path">The path.</param>
 /// <param name="isActive">if set to <c>true</c> [is active].</param>
 /// <param name="isDefault">if set to <c>true</c> [is default].</param>
 /// <param name="isExample">if set to <c>true</c> [is example].</param>
 /// <returns></returns>
 /// <remarks>Documented by Dev02, 2008-08-11</remarks>
 /// <remarks>Documented by Dev03, 2008-12-03</remarks>
 public IMedia CreateMedia(EMedia type, string path, bool isActive, bool isDefault, bool isExample)
 {
     throw new Exception("The method or operation is not implemented.");
 }
        private IList <int> GetCardMedia(int cardid, Side side, WordType?type)
        {
            IList <int>    ids       = new List <int>();
            IList <IMedia> mediaList = Parent.CurrentUser.Cache[ObjectLifetimeIdentifier.GetIdentifier(CacheObject.CardMedia, cardid)] as IList <IMedia>;

            if (mediaList != null)
            {
                foreach (DbMedia cms in mediaList)
                {
                    if (cms.Side == side && (!type.HasValue || cms.Type == type))
                    {
                        ids.Add(cms.Id);
                    }
                }

                return(ids);
            }

            using (NpgsqlConnection con = PostgreSQLConn.CreateConnection(Parent.CurrentUser))
            {
                using (NpgsqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = "SELECT id, media_type,side,type,is_default FROM \"Cards_MediaContent\" JOIN \"MediaContent\" ON media_id=id WHERE cards_id=:cardid;";
                    cmd.Parameters.Add("cardid", cardid);
                    NpgsqlDataReader reader = PostgreSQLConn.ExecuteReader(cmd, Parent.CurrentUser);

                    mediaList = new List <IMedia>();
                    while (reader.Read())
                    {
                        IMedia   newMedia  = null;
                        int      id        = Convert.ToInt32(reader["id"]);
                        EMedia   mtype     = (EMedia)Enum.Parse(typeof(EMedia), Convert.ToString(reader["media_type"]));
                        Side     cside     = (Side)Enum.Parse(typeof(Side), Convert.ToString(reader["side"]));
                        WordType wordtype  = (WordType)Enum.Parse(typeof(WordType), Convert.ToString(reader["type"]));
                        bool     isDefault = Convert.ToBoolean(reader["is_default"]);
                        switch (mtype)
                        {
                        case EMedia.Audio:
                            newMedia = new DbAudio(id, cardid, false, cside, wordtype, isDefault, (wordtype == WordType.Sentence), Parent);
                            break;

                        case EMedia.Image:
                            newMedia = new DbImage(id, cardid, false, cside, wordtype, isDefault, (wordtype == WordType.Sentence), Parent);
                            break;

                        case EMedia.Video:
                            newMedia = new DbVideo(id, cardid, false, cside, wordtype, isDefault, (wordtype == WordType.Sentence), Parent);
                            break;
                        }

                        mediaList.Add(newMedia);

                        if ((newMedia as DbMedia).Side == side && (!type.HasValue || (newMedia as DbMedia).Type == type))
                        {
                            ids.Add((newMedia as DbMedia).Id);
                        }
                    }

                    Parent.CurrentUser.Cache[ObjectLifetimeIdentifier.Create(CacheObject.CardMedia, cardid)] = mediaList;

                    return(ids);
                }
            }
        }
Ejemplo n.º 21
0
 /// <summary>
 /// Creates the new media object.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="path">The path.</param>
 /// <param name="isActive">if set to <c>true</c> [is active].</param>
 /// <param name="isDefault">if set to <c>true</c> [is default].</param>
 /// <param name="isExample">if set to <c>true</c> [is example].</param>
 /// <returns></returns>
 /// <remarks>Documented by Dev03, 2009-03-23</remarks>
 public static IMedia CreateNewMediaObject(EMedia type, string path, bool isActive, bool isDefault, bool isExample)
 {
     IMedia media;
     switch (type)
     {
         case EMedia.Audio:
             media = new PreviewAudio(path, isActive, isDefault, isExample, null);
             break;
         case EMedia.Image:
             media = new PreviewImage(path, isActive, isDefault, isExample, null);
             break;
         case EMedia.Video:
             media = new PreviewVideo(path, isActive, isDefault, isExample, null);
             break;
         default:
             media = new PreviewMedia(type, path, isActive, isDefault, isExample, null);
             break;
     }
     return media;
 }
        public void SetCardMedia(int id, int cardid, Side side, WordType type, bool isDefault, EMedia mediatype)
        {
            using (NpgsqlConnection con = PostgreSQLConn.CreateConnection(Parent.CurrentUser))
            {
                NpgsqlTransaction tran = con.BeginTransaction();
                ClearCardMedia(cardid, side, type, mediatype);

                using (NpgsqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = "INSERT INTO \"Cards_MediaContent\" (media_id, cards_id, side, type, is_default) VALUES (:mediaid, :cardid, :side, :type, :isdefault);";
                    cmd.Parameters.Add("mediaid", id);
                    cmd.Parameters.Add("cardid", cardid);
                    cmd.Parameters.Add("side", side.ToString());
                    cmd.Parameters.Add("type", type.ToString());
                    cmd.Parameters.Add("isdefault", isDefault);
                    PostgreSQLConn.ExecuteNonQuery(cmd, Parent.CurrentUser);
                }

                tran.Commit();

                Parent.CurrentUser.Cache.Uncache(ObjectLifetimeIdentifier.GetIdentifier(CacheObject.CardMedia, cardid));
            }
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Creates a new media object.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="path">The path.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <param name="isDefault">if set to <c>true</c> [is default].</param>
        /// <param name="isExample">if set to <c>true</c> [is example].</param>
        /// <returns></returns>
        internal IMedia CreateNewMediaObject(EMedia type, string path, bool isActive, bool isDefault, bool isExample)
        {
            IMedia media;
            Uri uri;

            if (path == null)
                throw new ArgumentNullException("Null value not allowed for media file path!");

            try
            {
                uri = new Uri(path);
            }
            catch (UriFormatException exception)
            {
                throw new FileNotFoundException("Uri format is invalid.", exception);
            }

            if (uri.Scheme == "http" && uri.IsLoopback)
            {
                //download the file
                WebClient client = new WebClient();
                byte[] buffer = client.DownloadData(uri);

                string extension = System.IO.Path.GetExtension(uri.ToString()); //the uri must contain the file extension
                path = FileCleanupQueue.GetTempFilePath(extension);

                File.WriteAllBytes(path, buffer);
            }

            if ((path.Trim().Length == 0) || !File.Exists(path))
                throw new FileNotFoundException("The media file does not exist at the given location!");

            switch (type)
            {
                case EMedia.Video:
                    media = new XmlVideo(path, isActive, parent);
                    break;
                case EMedia.Image:
                    media = new XmlImage(path, isActive, parent);
                    break;
                case EMedia.Audio:
                default:
                    media = new XmlAudio(path, isActive, isDefault, isExample, parent);
                    break;
            }
            return media;
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Creates a new media object.
        /// </summary>
        /// <param name="caller">The calling object.</param>
        /// <param name="rpu">A delegate of the type StatusMessageReportProgress.</param>
        /// <param name="type">The type.</param>
        /// <param name="path">The path.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <param name="isDefault">if set to <c>true</c> [is default].</param>
        /// <param name="isExample">if set to <c>true</c> [is example].</param>
        /// <returns></returns>
        /// <remarks>Documented by Dev02, 2008-08-11</remarks>
        internal IMedia CreateNewMediaObject(object caller, StatusMessageReportProgress rpu, EMedia type, string path, bool isActive, bool isDefault, bool isExample)
        {
            IMedia media = null;
            Uri    uri;

            if (!this.HasPermission(PermissionTypes.CanModifyMedia))
            {
                throw new PermissionException();
            }

            if (path == null)
            {
                throw new ArgumentNullException("Null value not allowed for media file path!");
            }

            try
            {
                if (File.Exists(Path.Combine(Environment.CurrentDirectory, path)))                 //to allow relative paths
                {
                    path = Path.Combine(Environment.CurrentDirectory, path);
                }

                uri = new Uri(path);
            }
            catch (UriFormatException exception)
            {
                throw new FileNotFoundException("Uri format is invalid.", exception);
            }

            if (uri.Scheme == Uri.UriSchemeFile && uri.IsFile)             //we got a new file
            {
                if (File.Exists(path))
                {
                    int newid;
                    using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                        newid = mediaconnector.CreateMedia(stream, type, rpu, caller);

                    media = DbMedia.CreateDisconnectedCardMedia(newid, type, isDefault, isExample, parent);

                    Helper.UpdateMediaProperties(path, newid, mediaconnector);
                }
                else
                {
                    throw new FileNotFoundException("Media file could not be found.", path);
                }
            }
            else if (uri.Scheme == "http" && uri.IsLoopback)             //we got a http reference => file is already in db
            {
                if (DbMediaServer.DbMediaServer.Instance(parent).IsYours(uri))
                {
                    int mediaId = DbMediaServer.DbMediaServer.GetMediaID(uri.AbsolutePath);
                    media = DbMedia.CreateDisconnectedCardMedia(mediaId, type, isDefault, isExample, parent);
                    rpu(new StatusMessageEventArgs(StatusMessageType.CreateMediaProgress, 100, 100), caller);
                }
                else
                {
                    DbMediaServer.DbMediaServer server = DbMediaServer.DbMediaServer.Instance(uri);

                    int newid = mediaconnector.CreateMedia(GetMediaConnector(server.Parent).GetMediaStream(DbMediaServer.DbMediaServer.GetMediaID(uri.AbsolutePath)), type, rpu, caller);

                    media = DbMedia.CreateDisconnectedCardMedia(newid, type, isDefault, isExample, parent);

                    Helper.UpdateMediaProperties(path, newid, mediaconnector);
                }
            }

            return(media);
        }
        public void ClearCardMedia(int cardid, Side side, WordType type, EMedia mediatype)
        {
            using (SqlCeCommand cmd = MSSQLCEConn.CreateCommand(Parent.CurrentUser))
            {
                cmd.CommandText = "DELETE FROM \"Cards_MediaContent\" WHERE cards_id=@cardid AND side=@side AND type=@type " +
                    "AND media_id IN (SELECT id FROM \"MediaContent\" WHERE media_type=@mediatype);";
                cmd.Parameters.Add("@cardid", cardid);
                cmd.Parameters.Add("@side", side.ToString());
                cmd.Parameters.Add("@type", type.ToString());
                cmd.Parameters.Add("@mediatype", mediatype.ToString());
                cmd.ExecuteNonQuery();
            }

            Parent.CurrentUser.Cache.Uncache(ObjectLifetimeIdentifier.GetIdentifier(CacheObject.CardMedia, cardid));
        }
Ejemplo n.º 26
0
 /// <summary>
 /// Creates a new media object.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="path">The path.</param>
 /// <param name="isActive">if set to <c>true</c> [is active].</param>
 /// <param name="isDefault">if set to <c>true</c> [is default].</param>
 /// <param name="isExample">if set to <c>true</c> [is example].</param>
 /// <returns></returns>
 /// <remarks>Documented by Dev02, 2008-08-11</remarks>
 /// <remarks>Documented by Dev03, 2008-12-03</remarks>
 public IMedia CreateMedia(EMedia type, string path, bool isActive, bool isDefault, bool isExample)
 {
     throw new Exception("The method or operation is not implemented.");
 }
Ejemplo n.º 27
0
        /// <summary>
        /// Adds the Media to the spezified card.
        /// </summary>
        /// <param name="cardID">The card ID.</param>
        /// <param name="filename">The filename.</param>
        /// <param name="type">The type.</param>
        /// <param name="side">The side.</param>
        /// <param name="active">if set to <c>true</c> the file is active.</param>
        /// <param name="standard">if set to <c>true</c> the file is the default file.</param>
        /// <param name="example">if set to <c>true</c> the file is the example.</param>
        /// <remarks>Documented by Dev05, 2007-09-03</remarks>
        public void AddMedia(int cardID, string filename, EMedia type, Side side, bool active, bool standard, bool example)
        {
            ICard card = cards.Get(cardID);

            card.AddMedia(card.CreateMedia(type, filename, active, standard, example), side);
        }
Ejemplo n.º 28
0
        /// <summary>
        /// Loads dictionary from the old database format (ODF) and stores it to the current format.
        /// </summary>
        /// <param name="srcFile">The source database file.</param>
        /// <param name="dstFile">The destination database file.</param>
        /// <remarks>Documented by Dev03, 2007-07-26</remarks>
        public MLifter.DAL.Interfaces.IDictionary Load(string srcFile, string dstFile)
        {
            MLifter.DAL.Interfaces.IDictionary dictionary = null;
            int wbVersion;

#if !DEBUG
            try
            {
#endif
            string srcPath            = Path.GetDirectoryName(srcFile);
            _rBox_[] box_data         = new _rBox_[0];
            _rChapter_[] chapter_data = new _rChapter_[0];
            _rCards_[] cards_data     = new _rCards_[0];
            _rBoxes_[] boxes_data     = new _rBoxes_[0];
            _rBlob_[] blob_data       = new _rBlob_[0];
            _rStats_[] stats_data     = new _rStats_[0];

            string headerstr = string.Empty;
            UInt16 ssize;
            int len, rsize;

            using (FileStream DicFile = new FileStream(srcFile, FileMode.Open))
            {
                BinaryReader Dic = new BinaryReader(DicFile, System.Text.UnicodeEncoding.UTF8);
                // Check header first
                len = Dic.ReadInt32();
                if (len < 30)
                {
                    headerstr = byte2string(Dic.ReadBytes(len));
                }
                else
                {
                    headerstr = string.Empty;
                }
                // Check version byte
                wbVersion = Dic.ReadInt16();

                // Some code to correct "bugs" in previous dictionary versions
                if (wbVersion <= 0x0104)
                {
                    throw new DictionaryFormatNotSupported(wbVersion);
                }

                // Header has to be okay, and Version smaller than prog's version
                if (headerstr.Equals(m_odfFileHeaderString) && (wbVersion <= m_oldFileVersion))
                {
                    if (File.Exists(dstFile))
                    {
                        throw new DictionaryPathExistsException(dstFile);
                    }

                    IUser user = UserFactory.Create(m_loginCallback, new ConnectionStringStruct(DatabaseType.Xml, dstFile, false),
                                                    (DataAccessErrorDelegate) delegate { return; }, this);
                    dictionary = user.Open();
                    len        = Dic.ReadByte();
                    dictionary.DefaultSettings.QuestionCaption = byte2string(Dic.ReadBytes(20)).Substring(0, len);
                    len = Dic.ReadByte();
                    dictionary.DefaultSettings.AnswerCaption = byte2string(Dic.ReadBytes(20)).Substring(0, len);

                    Dic.ReadBytes(82);                    //SrcFont & DstFont
                    Dic.ReadBytes(2);                     //SrcCharSet & DstCharSet

                    Dic.ReadBoolean();                    //ReadOnly

                    ssize             = Dic.ReadUInt16();
                    dictionary.Author = byte2string(Dic.ReadBytes(ssize));

                    ssize = Dic.ReadUInt16();
                    dictionary.Description = byte2string(Dic.ReadBytes(ssize));

                    if (wbVersion >= 0x0104)
                    {
                        ssize = Dic.ReadUInt16();
                        string mediafolder = byte2string(Dic.ReadBytes(ssize)).TrimEnd(new char[] { Path.DirectorySeparatorChar });
                        if (Directory.Exists(mediafolder))
                        {
                            dictionary.MediaDirectory = mediafolder;
                        }
                    }

                    if (wbVersion >= 0x0107)
                    {
                        for (int i = 0; i < 12; i++)
                        {
                            ssize = Dic.ReadUInt16();
                            dictionary.DefaultSettings.CommentarySounds[CommentarySoundIdentifier.Create(i > 6 ? Side.Answer : Side.Question,
                                                                                                         (ECommentarySoundType)(i > 6 ? i - 6 : i))] =
                                new MLifter.DAL.XML.XmlAudio(SkinDir2RealDir(byte2string(Dic.ReadBytes(ssize)), Path.GetDirectoryName(dstFile), m_applicationPath),
                                                             new MLifter.DAL.Tools.ParentClass(user, dictionary));
                        }
                    }

                    if (wbVersion >= 0x0108)
                    {
                        Dic.ReadBytes(10);                         //Pwd
                    }
                    if (wbVersion >= 0x0109)
                    {
                        dictionary.Category = new Category((int)Dic.ReadByte(), false);
                    }
                    else
                    {
                        dictionary.Category = new Category(m_defaultCategory, true);
                    }

                    (dictionary as MLifter.DAL.XML.XmlDictionary).score = Dic.ReadInt32();
                    dictionary.HighScore = Dic.ReadInt32();

                    int queryChapter = 0;
                    rsize = Dic.ReadInt32();
                    dictionary.DefaultSettings.SelectedLearnChapters.Clear();
                    for (int i = 0; i < rsize; i++)
                    {
                        queryChapter = Math.Abs(Dic.ReadInt32());     // BUG?? what is the minus for??
                        if (queryChapter < 0)                         //chapterID starts with 1, should be zero-based
                        {
                            queryChapter++;
                        }
                        else
                        {
                            queryChapter--;
                        }

                        dictionary.DefaultSettings.SelectedLearnChapters.Add(queryChapter);
                    }

                    Dic.ReadUInt16();                       //the LastBox user property is not used anymore
                    EQueryDirection dir = (EQueryDirection)Dic.ReadByte();
                    switch (dir)
                    {
                    case EQueryDirection.Question2Answer:
                        dictionary.DefaultSettings.QueryDirections.Question2Answer = true;
                        break;

                    case EQueryDirection.Answer2Question:
                        dictionary.DefaultSettings.QueryDirections.Answer2Question = true;
                        break;

                    case EQueryDirection.Mixed:
                        dictionary.DefaultSettings.QueryDirections.Mixed = true;
                        break;
                    }

                    int queryType = Convert.ToInt32(Dic.ReadByte());
                    dictionary.DefaultSettings.QueryTypes.ImageRecognition       = ((((int)EQueryType.ImageRecognition) & queryType) > 0);
                    dictionary.DefaultSettings.QueryTypes.ListeningComprehension = ((((int)EQueryType.ListeningComprehension) & queryType) > 0);
                    dictionary.DefaultSettings.QueryTypes.MultipleChoice         = ((((int)EQueryType.MultipleChoice) & queryType) > 0);
                    dictionary.DefaultSettings.QueryTypes.Sentence = ((((int)EQueryType.Sentences) & queryType) > 0);
                    dictionary.DefaultSettings.QueryTypes.Word     = ((((int)EQueryType.Word) & queryType) > 0);

                    int queryOptions;
                    if (wbVersion >= 0x0103)
                    {
                        queryOptions = Convert.ToInt32(Dic.ReadUInt16());
                    }
                    else
                    {
                        queryOptions = Convert.ToInt32(Dic.ReadByte());
                    }

                    dictionary.DefaultSettings.CaseSensitive      = ((((int)EQueryOption.CaseSensitive) & queryOptions) > 0);
                    dictionary.DefaultSettings.EnableTimer        = ((((int)EQueryOption.CountDown) & queryOptions) > 0);
                    dictionary.DefaultSettings.ShowStatistics     = ((((int)EQueryOption.Stats) & queryOptions) > 0);
                    dictionary.DefaultSettings.ShowImages         = ((((int)EQueryOption.Images) & queryOptions) > 0);
                    dictionary.DefaultSettings.AutoplayAudio      = ((((int)EQueryOption.Sounds) & queryOptions) > 0);
                    dictionary.DefaultSettings.EnableCommentary   = ((((int)EQueryOption.Commentary) & queryOptions) > 0);
                    dictionary.DefaultSettings.CorrectOnTheFly    = ((((int)EQueryOption.Correct) & queryOptions) > 0);
                    dictionary.DefaultSettings.SkipCorrectAnswers = ((((int)EQueryOption.Skip) & queryOptions) > 0);
                    dictionary.DefaultSettings.SelfAssessment     = ((((int)EQueryOption.Self) & queryOptions) > 0);
                    dictionary.DefaultSettings.RandomPool         = ((((int)EQueryOption.RandomPool) & queryOptions) > 0);
                    dictionary.DefaultSettings.ConfirmDemote      = ((((int)EQueryOption.ConfirmDemote) & queryOptions) > 0);

                    if (wbVersion >= 0x0101)
                    {
                        EGradeSynonyms gradeS = (EGradeSynonyms)Dic.ReadByte();
                        switch (gradeS)
                        {
                        case EGradeSynonyms.AllKnown:
                            dictionary.DefaultSettings.GradeSynonyms.AllKnown = true;
                            break;

                        case EGradeSynonyms.HalfKnown:
                            dictionary.DefaultSettings.GradeSynonyms.HalfKnown = true;
                            break;

                        case EGradeSynonyms.OneKnown:
                            dictionary.DefaultSettings.GradeSynonyms.OneKnown = true;
                            break;

                        case EGradeSynonyms.FirstKnown:
                            dictionary.DefaultSettings.GradeSynonyms.FirstKnown = true;
                            break;

                        case EGradeSynonyms.Prompt:
                            dictionary.DefaultSettings.GradeSynonyms.Prompt = true;
                            break;
                        }
                    }
                    else
                    {
                        dictionary.DefaultSettings.GradeSynonyms.OneKnown = true;
                    }

                    dictionary.DefaultSettings.PoolEmptyMessageShown = Dic.ReadBoolean();
                    dictionary.DefaultSettings.UseLMStylesheets      = false;

                    ssize = Dic.ReadUInt16();
                    Dic.ReadBytes(ssize);                  //Skin
                    Dic.ReadBoolean();                     //UseSkin

                    string stripchars = String.Empty;
                    if (wbVersion >= 0x0102)
                    {
                        byte[] temp = Dic.ReadBytes(32);
                        for (int i = 0; i < 8; i++)
                        {
                            for (int j = 0; j < 8; j++)
                            {
                                if ((temp[i] & (0x0001 << j)) > 0)
                                {
                                    stripchars += (char)(i * 8 + j);
                                }
                            }
                        }
                        dictionary.DefaultSettings.StripChars = stripchars;
                    }

                    EGradeTyping gradeT = (EGradeTyping)Dic.ReadByte();
                    switch (gradeT)
                    {
                    case EGradeTyping.AllCorrect:
                        dictionary.DefaultSettings.GradeTyping.AllCorrect = true;
                        break;

                    case EGradeTyping.HalfCorrect:
                        dictionary.DefaultSettings.GradeTyping.HalfCorrect = true;
                        break;

                    case EGradeTyping.NoneCorrect:
                        dictionary.DefaultSettings.GradeTyping.NoneCorrect = true;
                        break;

                    case EGradeTyping.Prompt:
                        dictionary.DefaultSettings.GradeTyping.Prompt = true;
                        break;

                    default:
                        break;
                    }

                    if ((((int)ESnoozeMode.QuitProgram) & queryOptions) > 0)
                    {
                        dictionary.DefaultSettings.SnoozeOptions.SnoozeMode = ESnoozeMode.QuitProgram;
                    }
                    if ((((int)ESnoozeMode.SendToTray) & queryOptions) > 0)
                    {
                        dictionary.DefaultSettings.SnoozeOptions.SnoozeMode = ESnoozeMode.SendToTray;
                    }

                    if (!ReportProgressUpdate(5))
                    {
                        return(null);
                    }

                    if (wbVersion >= 0x0103)
                    {
                        int snoozeTime     = Convert.ToInt32(Dic.ReadByte());
                        int snoozeRights   = Convert.ToInt32(Dic.ReadByte());
                        int snoozeCards    = Convert.ToInt32(Dic.ReadByte());
                        int snoozeTimeLow  = Convert.ToInt32(Dic.ReadByte());
                        int snoozeTimeHigh = Convert.ToInt32(Dic.ReadByte());
                        if ((((int)ESnoozeMode.Time) & queryType) > 0)
                        {
                            dictionary.DefaultSettings.SnoozeOptions.EnableTime(snoozeTime);
                        }
                        if ((((int)ESnoozeMode.Rights) & queryType) > 0)
                        {
                            dictionary.DefaultSettings.SnoozeOptions.EnableRights(snoozeRights);
                        }
                        if ((((int)ESnoozeMode.Cards) & queryType) > 0)
                        {
                            dictionary.DefaultSettings.SnoozeOptions.EnableCards(snoozeCards);
                        }
                        dictionary.DefaultSettings.SnoozeOptions.SetSnoozeTimes(snoozeTimeLow, snoozeTimeHigh);
                    }
                    //box_data -> user
                    rsize    = Dic.ReadInt32();
                    box_data = (_rBox_[])SetLength(box_data, rsize);
                    for (int i = 0; i < box_data.Length; i++)
                    {
                        box_data[i]       = new _rBox_();
                        box_data[i].First = Dic.ReadInt32();
                        Dic.ReadInt32();                         // box_data[i].Last

                        if (i < box_data.Length - 2)
                        {
                            dictionary.Boxes.Box[i].MaximalSize = Dic.ReadInt32();
                        }
                        else
                        {
                            Dic.ReadInt32();                             //MaxLen for the last two boxes - not needed
                        }

                        if (wbVersion > 0x0106)
                        {
                            Dic.ReadInt32();                             // box_data[i].Len
                        }
                    }
                    // end of user -----------------------------------------------------------------

                    if (!ReportProgressUpdate(10))
                    {
                        return(null);
                    }

                    //chapter_data
                    rsize        = Dic.ReadInt32();
                    chapter_data = (_rChapter_[])SetLength(chapter_data, rsize);
                    for (int i = 0; i < chapter_data.Length; i++)
                    {
                        chapter_data[i] = new _rChapter_();
                        Dic.ReadUInt16();                         //ID
                        Dic.ReadUInt16();                         // SubID
                        len = Dic.ReadByte();
                        chapter_data[i].Title = byte2string(Dic.ReadBytes(30)).Substring(0, len);
                        len = Dic.ReadByte();
                        chapter_data[i].Description = byte2string(Dic.ReadBytes(256)).Substring(0, len);
                    }

                    if (!ReportProgressUpdate(15))
                    {
                        return(null);
                    }

                    //cards_data
                    rsize      = Dic.ReadInt32();
                    cards_data = (_rCards_[])SetLength(cards_data, rsize);
                    for (int i = 0; i < cards_data.Length; i++)
                    {
                        cards_data[i] = new _rCards_();
                        ssize         = Dic.ReadUInt16();
                        cards_data[i].Src.SpecialImportFormat = byte2string(Dic.ReadBytes(ssize));
                        ssize = Dic.ReadUInt16();
                        cards_data[i].Dst.SpecialImportFormat = byte2string(Dic.ReadBytes(ssize));
                        ssize = Dic.ReadUInt16();
                        cards_data[i].SentSrc = byte2string(Dic.ReadBytes(ssize));
                        ssize = Dic.ReadUInt16();
                        cards_data[i].SentDst   = byte2string(Dic.ReadBytes(ssize));
                        cards_data[i].ChapterID = Dic.ReadUInt16() - 1;                         //chapterID starts with 1, should be zero-based
                        cards_data[i].CardParts = Dic.ReadByte();
                        if (wbVersion >= 0x0107)
                        {
                            Dic.ReadByte();                             // Probability
                        }
                    }

                    if (!ReportProgressUpdate(20))
                    {
                        return(null);
                    }

                    //boxes_data
                    rsize      = Dic.ReadInt32();
                    boxes_data = (_rBoxes_[])SetLength(boxes_data, rsize);
                    for (int i = 0; i < boxes_data.Length; i++)
                    {
                        boxes_data[i]       = new _rBoxes_();
                        boxes_data[i].BoxID = Dic.ReadByte();
                        Dic.ReadBytes(3);                         //TODO BUG ?
                        boxes_data[i].CardID = Dic.ReadInt32();
                        boxes_data[i].Prior  = Dic.ReadInt32();
                        boxes_data[i].Next   = Dic.ReadInt32();
                    }

                    if (!ReportProgressUpdate(25))
                    {
                        return(null);
                    }

                    //blob_data
                    rsize     = Dic.ReadInt32();
                    blob_data = (_rBlob_[])SetLength(blob_data, rsize);
                    for (int i = 0; i < blob_data.Length; i++)
                    {
                        blob_data[i]        = new _rBlob_();
                        blob_data[i].SrcDst = (TBlobType)Dic.ReadByte();
                        len = Dic.ReadByte();
                        string tstr = byte2string(Dic.ReadBytes(101));
                        blob_data[i].Link = tstr.Substring(0, len);
                        blob_data[i].Link = blob_data[i].Link.Replace('/', '\\');                         // repair work
                        blob_data[i].Link = blob_data[i].Link.Replace("\\\\", "\\");                      // repair work
                        Dic.ReadByte();
                        blob_data[i].CardID = Dic.ReadInt32();
                    }

                    if (!ReportProgressUpdate(30))
                    {
                        return(null);
                    }

                    //stats_data
                    rsize      = Dic.ReadInt32();
                    stats_data = (_rStats_[])SetLength(stats_data, rsize);
                    for (int i = 0; i < stats_data.Length; i++)
                    {
                        stats_data[i]        = new _rStats_(m_nrBox);
                        stats_data[i].SStart = new System.DateTime(1899, 12, 30, 0, 0, 0);                       // startdate in delphi
                        stats_data[i].SStart = stats_data[i].SStart.AddDays(Dic.ReadDouble());
                        stats_data[i].SEnd   = new System.DateTime(1899, 12, 30, 0, 0, 0);                       // startdate in delphi
                        stats_data[i].SEnd   = stats_data[i].SEnd.AddDays(Dic.ReadDouble());
                        stats_data[i].Right  = Dic.ReadInt32();
                        stats_data[i].Wrong  = Dic.ReadInt32();
                        for (int j = 0; j < m_nrBox; j++)
                        {
                            stats_data[i].Boxes[j] = Dic.ReadInt32();
                        }
                    }

                    if (!ReportProgressUpdate(35))
                    {
                        return(null);
                    }

                    //chapters
                    for (int i = 0; i < chapter_data.Length; i++)
                    {
                        IChapter chapter = dictionary.Chapters.AddNew();
                        //chapter.Id = i; //Interface change
                        chapter.Title       = chapter_data[i].Title;
                        chapter.Description = chapter_data[i].Description;
                    }

                    if (!ReportProgressUpdate(40))
                    {
                        return(null);
                    }

                    //cards
                    for (int i = 0; i < cards_data.Length; i++)
                    {
                        if ((i % 10) == 0)
                        {
                            int progress = (int)Math.Floor(35.0 + ((90.0 - 35.0) * (i + 1) / cards_data.Length));
                            if (!ReportProgressUpdate(progress))
                            {
                                return(null);
                            }
                        }
                        ICard card = dictionary.Cards.AddNew();
                        //card.Id = i;  //Interface change
                        StringList words = new StringList();
                        words.CommaText = cards_data[i].Src.CommaText;
                        for (int k = 0; k < words.Count; k++)
                        {
                            IWord word = card.Question.CreateWord(words[k], WordType.Word, (k == 0));
                            card.Question.AddWord(word);
                        }
                        card.QuestionExample.AddWord(card.QuestionExample.CreateWord(cards_data[i].SentSrc, WordType.Sentence, false));
                        words.CommaText = cards_data[i].Dst.CommaText;
                        for (int k = 0; k < words.Count; k++)
                        {
                            IWord word = card.Answer.CreateWord(words[k], WordType.Word, (k == 0));
                            card.Answer.AddWord(word);
                        }
                        card.AnswerExample.AddWord(card.AnswerExample.CreateWord(cards_data[i].SentDst, WordType.Sentence, false));
                        card.Chapter   = cards_data[i].ChapterID;
                        card.Box       = 0;
                        card.Timestamp = DateTime.Now;

                        for (int j = 0; j < blob_data.Length; j++)
                        {
                            if (blob_data[j].CardID == i)
                            {
                                if (File.Exists(Path.Combine(srcPath, blob_data[j].Link)))
                                {
                                    blob_data[j].Link = Path.Combine(srcPath, blob_data[j].Link);
                                    if (blob_data[j].SrcDst.ToString().Equals("image"))
                                    {
                                        IMedia media = card.CreateMedia(EMedia.Image, blob_data[j].Link, true, false, false);
                                        card.AddMedia(media, Side.Question);
                                        card.AddMedia(media, Side.Answer);
                                    }
                                    else
                                    {
                                        IMedia media = null;
                                        Side   side  = Side.Question;
                                        switch (blob_data[j].SrcDst.ToString())
                                        {
                                        case "questionaudio":
                                            media = card.CreateMedia(EMedia.Audio, blob_data[j].Link, true, true, false);
                                            side  = Side.Question;
                                            break;

                                        case "questionexampleaudio":
                                            media = card.CreateMedia(EMedia.Audio, blob_data[j].Link, true, false, true);
                                            side  = Side.Question;
                                            break;

                                        case "questionvideo":
                                            media = card.CreateMedia(EMedia.Video, blob_data[j].Link, true, false, false);
                                            side  = Side.Question;
                                            break;

                                        case "questionimage":
                                            media = card.CreateMedia(EMedia.Image, blob_data[j].Link, true, false, false);
                                            side  = Side.Question;
                                            break;

                                        case "answeraudio":
                                            media = card.CreateMedia(EMedia.Audio, blob_data[j].Link, true, true, false);
                                            side  = Side.Answer;
                                            break;

                                        case "answerexampleaudio":
                                            media = card.CreateMedia(EMedia.Audio, blob_data[j].Link, true, false, true);
                                            side  = Side.Answer;
                                            break;

                                        case "answervideo":
                                            media = card.CreateMedia(EMedia.Video, blob_data[j].Link, true, false, false);
                                            side  = Side.Answer;
                                            break;

                                        case "answerimage":
                                            media = card.CreateMedia(EMedia.Image, blob_data[j].Link, true, false, false);
                                            side  = Side.Answer;
                                            break;

                                        case "unusedmedia":
                                        default:
                                            try
                                            {
                                                EMedia mType = Helper.GetMediaType(blob_data[j].Link);
                                                if (mType == EMedia.Unknown)
                                                {
                                                    media = card.CreateMedia(mType, blob_data[j].Link, false, false, false);
                                                    side  = Side.Question;                                                               //doesn't matter
                                                }
                                            }
                                            catch { }
                                            break;
                                        }
                                        if (media != null)
                                        {
                                            card.AddMedia(media, side);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (!ReportProgressUpdate(95))
                    {
                        return(null);
                    }

                    //stats
                    for (int i = 0; i < stats_data.Length; i++)
                    {
                        IStatistic stat = new MLifter.DAL.XML.XmlStatistic(dictionary as MLifter.DAL.XML.XmlDictionary, i, true);
                        dictionary.Statistics.Add(stat);
                        stat.StartTimestamp = stats_data[i].SStart;
                        stat.EndTimestamp   = stats_data[i].SEnd;
                        stat.Right          = stats_data[i].Right;
                        stat.Wrong          = stats_data[i].Wrong;
                        stat.Boxes.Clear();
                        for (int j = 0; j < stats_data[i].Boxes.Length; j++)
                        {
                            stat.Boxes.Add(stats_data[i].Boxes[j]);
                        }
                    }

                    DateTime start_of_year      = new DateTime(m_releaseYear, 1, 1);
                    int      additional_seconds = 1;

                    for (int i = box_data.Length - 1; i >= 0; i--)
                    {
                        int exitCounter = 0;
                        for (int j = box_data[i].First; j != (-1); j = boxes_data[j].Next)
                        {
                            try
                            {
                                ICard card = dictionary.Cards.Get(boxes_data[j].CardID);
                                card.Box       = boxes_data[j].BoxID;
                                card.Timestamp = start_of_year.AddSeconds(additional_seconds++).ToUniversalTime();
                            }
                            catch { }
                            //boxes_data seems to be a linked list which sometimes may be broken - this is the emergency exit
                            if (exitCounter++ > dictionary.Cards.Count)
                            {
                                break;
                            }
                        }
                    }

                    dictionary.Save();
                }
                else
                {
                    throw new InvalidImportFormatException(dstFile);
                }

                Dic.Close();
            }
#if !DEBUG
        }

        catch (DictionaryPathExistsException ex)
        {
            throw ex;
        }
        catch (DictionaryFormatNotSupported ex)
        {
            throw ex;
        }
        catch (InvalidImportFormatException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw ex;
        }
#endif
            return(dictionary);
        }
Ejemplo n.º 29
0
 /// <summary>
 /// A helper function to create Media objects.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="filename">The filename.</param>
 /// <param name="isActive">if set to <c>true</c> [is active].</param>
 /// <param name="isDefault">if set to <c>true</c> [is default].</param>
 /// <param name="isExample">if set to <c>true</c> [is example].</param>
 /// <returns></returns>
 /// <remarks>Documented by Dev02, 2008-08-11</remarks>
 private IMedia CreateMedia(EMedia type, string filename, bool isActive, bool isDefault, bool isExample)
 {
     IMedia media = (new MLifter.DAL.Preview.PreviewCard(null)).CreateMedia(type, filename, isActive, isDefault, isExample);
     return media;
 }
Ejemplo n.º 30
0
 /// <summary>
 /// Creates the media.
 /// </summary>
 /// <param name="type">The type of the media file.</param>
 /// <param name="path">The path to the media file.</param>
 /// <param name="isActive">if set to <c>true</c> [is active].</param>
 /// <param name="isDefault">if set to <c>true</c> [is default].</param>
 /// <param name="isExample">if set to <c>true</c> [is example].</param>
 /// <returns></returns>
 /// <remarks>Documented by Dev03, 2007-09-03</remarks>
 /// <remarks>Documented by Dev03, 2009-01-13</remarks>
 public IMedia CreateMedia(EMedia type, string path, bool isActive, bool isDefault, bool isExample)
 {
     StatusMessageReportProgress rpu = new StatusMessageReportProgress(ReportProgressUpdate);
     return (parent.GetParentDictionary() as DbDictionary).CreateNewMediaObject(this, rpu, type, path, isActive, isDefault, isExample);
 }
Ejemplo n.º 31
0
 public PreviewMedia(EMedia type, string path, bool isActive, bool isDefault, bool isExample, ParentClass parent)
 {
     this.parent = parent;
     this.mediatype = type;
     this.filename = path;
     this.active = isActive;
     this._default = isDefault;
     this.example = isExample;
 }
        public void ClearCardMedia(int cardid, Side side, WordType type, EMedia mediatype)
        {
            using (NpgsqlConnection con = PostgreSQLConn.CreateConnection(Parent.CurrentUser))
            {
                using (NpgsqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = "DELETE FROM \"Cards_MediaContent\" WHERE cards_id=:cardid AND side=:side AND type=:type " +
                        "AND media_id IN (SELECT id FROM \"MediaContent\" WHERE media_type=:mediatype);";
                    cmd.Parameters.Add("cardid", cardid);
                    cmd.Parameters.Add("side", side.ToString());
                    cmd.Parameters.Add("type", type.ToString());
                    cmd.Parameters.Add("mediatype", mediatype.ToString());
                    cmd.ExecuteNonQuery();
                }
            }

            Parent.CurrentUser.Cache.Uncache(ObjectLifetimeIdentifier.GetIdentifier(CacheObject.CardMedia, cardid));
        }
        public void SetCardMedia(int id, int cardid, Side side, WordType type, bool isDefault, EMedia mediatype)
        {
            using (NpgsqlConnection con = PostgreSQLConn.CreateConnection(Parent.CurrentUser))
            {
                NpgsqlTransaction tran = con.BeginTransaction();
                ClearCardMedia(cardid, side, type, mediatype);

                using (NpgsqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = "INSERT INTO \"Cards_MediaContent\" (media_id, cards_id, side, type, is_default) VALUES (:mediaid, :cardid, :side, :type, :isdefault);";
                    cmd.Parameters.Add("mediaid", id);
                    cmd.Parameters.Add("cardid", cardid);
                    cmd.Parameters.Add("side", side.ToString());
                    cmd.Parameters.Add("type", type.ToString());
                    cmd.Parameters.Add("isdefault", isDefault);
                    PostgreSQLConn.ExecuteNonQuery(cmd, Parent.CurrentUser);
                }

                tran.Commit();

                Parent.CurrentUser.Cache.Uncache(ObjectLifetimeIdentifier.GetIdentifier(CacheObject.CardMedia, cardid));
            }
        }
Ejemplo n.º 34
0
 /// <summary>
 /// Creates the media.
 /// </summary>
 /// <param name="type">The type of the media file.</param>
 /// <param name="path">The path to the media file.</param>
 /// <param name="isActive">if set to <c>true</c> [is active].</param>
 /// <param name="isDefault">if set to <c>true</c> [is default].</param>
 /// <param name="isExample">if set to <c>true</c> [is example].</param>
 /// <returns></returns>
 /// <remarks>Documented by Dev03, 2007-09-03</remarks>
 /// <remarks>Documented by Dev03, 2009-03-23</remarks>
 public IMedia CreateMedia(EMedia type, string path, bool isActive, bool isDefault, bool isExample)
 {
     return(CreateNewMediaObject(type, path, isActive, isDefault, isExample));
 }
Ejemplo n.º 35
0
 /// <summary>
 /// Creates a new Media object.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="path">The path.</param>
 /// <param name="isActive">if set to <c>true</c> [is active].</param>
 /// <param name="isDefault">if set to <c>true</c> [is default].</param>
 /// <param name="isExample">if set to <c>true</c> [is example].</param>
 /// <returns></returns>
 /// <remarks>Documented by Dev02, 2008-08-11</remarks>
 public IMedia CreateMedia(EMedia type, string path, bool isActive, bool isDefault, bool isExample)
 {
     return dictionary.CreateMedia(type, path, IsActive, isDefault, isExample);
 }
Ejemplo n.º 36
0
 /// <summary>
 /// Creates a new media object.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="path">The path.</param>
 /// <param name="isActive">if set to <c>true</c> [is active].</param>
 /// <param name="isDefault">if set to <c>true</c> [is default].</param>
 /// <param name="isExample">if set to <c>true</c> [is example].</param>
 /// <returns></returns>
 /// <remarks>Documented by Dev02, 2008-08-11</remarks>
 /// <remarks>Documented by Dev03, 2008-12-03</remarks>
 public IMedia CreateMedia(EMedia type, string path, bool isActive, bool isDefault, bool isExample)
 {
     return CreateNewMediaObject(type, path, isActive, isDefault, isExample);
 }
Ejemplo n.º 37
0
 protected XmlMedia(EMedia mediaType, string filename, ParentClass parent)
 {
     this.parent       = parent;
     m_mediaIdentifier = mediaType;
     m_filename        = filename;
 }
Ejemplo n.º 38
0
        /// <summary>
        /// Creates a new media object.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="path">The path.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <param name="isDefault">if set to <c>true</c> [is default].</param>
        /// <param name="isExample">if set to <c>true</c> [is example].</param>
        /// <returns></returns>
        /// <remarks>Documented by Dev02, 2008-08-11</remarks>
        /// <remarks>Documented by Dev03, 2009-01-13</remarks>
        public IMedia CreateMedia(EMedia type, string path, bool isActive, bool isDefault, bool isExample)
        {
            StatusMessageReportProgress rpu = new StatusMessageReportProgress(ReportProgressUpdate);

            return(CreateNewMediaObject(this, rpu, type, path, isActive, isDefault, isExample));
        }
Ejemplo n.º 39
0
        /// <summary>
        /// Creates a new media object.
        /// </summary>
        /// <param name="caller">The calling object.</param>
        /// <param name="rpu">A delegate of the type StatusMessageReportProgress.</param>
        /// <param name="type">The type.</param>
        /// <param name="path">The path.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <param name="isDefault">if set to <c>true</c> [is default].</param>
        /// <param name="isExample">if set to <c>true</c> [is example].</param>
        /// <returns></returns>
        /// <remarks>Documented by Dev02, 2008-08-11</remarks>
        internal IMedia CreateNewMediaObject(object caller, StatusMessageReportProgress rpu, EMedia type, string path, bool isActive, bool isDefault, bool isExample)
        {
            IMedia media = null;
            Uri uri;

            if (!this.HasPermission(PermissionTypes.CanModifyMedia))
                throw new PermissionException();

            if (path == null)
                throw new ArgumentNullException("Null value not allowed for media file path!");

            try
            {
                if (File.Exists(Path.Combine(Environment.CurrentDirectory, path))) //to allow relative paths
                    path = Path.Combine(Environment.CurrentDirectory, path);

                uri = new Uri(path);
            }
            catch (UriFormatException exception)
            {
                throw new FileNotFoundException("Uri format is invalid.", exception);
            }

            if (uri.Scheme == Uri.UriSchemeFile && uri.IsFile) //we got a new file
            {
                if (File.Exists(path))
                {
                    int newid;
                    using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                        newid = mediaconnector.CreateMedia(stream, type, rpu, caller);

                    media = DbMedia.CreateDisconnectedCardMedia(newid, type, isDefault, isExample, parent);

                    Helper.UpdateMediaProperties(path, newid, mediaconnector);
                }
                else
                    throw new FileNotFoundException("Media file could not be found.", path);
            }
            else if (uri.Scheme == "http" && uri.IsLoopback) //we got a http reference => file is already in db
            {
                if (DbMediaServer.DbMediaServer.Instance(parent).IsYours(uri))
                {
                    int mediaId = DbMediaServer.DbMediaServer.GetMediaID(uri.AbsolutePath);
                    media = DbMedia.CreateDisconnectedCardMedia(mediaId, type, isDefault, isExample, parent);
                    rpu(new StatusMessageEventArgs(StatusMessageType.CreateMediaProgress, 100, 100), caller);
                }
                else
                {
                    DbMediaServer.DbMediaServer server = DbMediaServer.DbMediaServer.Instance(uri);

                    int newid = mediaconnector.CreateMedia(GetMediaConnector(server.Parent).GetMediaStream(DbMediaServer.DbMediaServer.GetMediaID(uri.AbsolutePath)), type, rpu, caller);

                    media = DbMedia.CreateDisconnectedCardMedia(newid, type, isDefault, isExample, parent);

                    Helper.UpdateMediaProperties(path, newid, mediaconnector);
                }
            }

            return media;
        }
Ejemplo n.º 40
0
        /// <summary>
        /// Creates a new media object.
        /// </summary>
        /// <param name="media">The memory stream containing the media.</param>
        /// <param name="type">The media type.</param>
        /// <param name="rpu">A delegate of type <see cref="StatusMessageReportProgress"/> used to send messages back to the calling object.</param>
        /// <param name="caller">The calling object.</param>
        /// <returns>The id for the new media object.</returns>
        /// <remarks>Documented by Dev03, 2008-08-05</remarks>
        /// <remarks>Documented by Dev03, 2009-01-13</remarks>
        public int CreateMedia(Stream media, EMedia type, StatusMessageReportProgress rpu, object caller)
        {
            using (NpgsqlConnection conn = PostgreSQLConn.CreateConnection(Parent.CurrentUser))
            {
                NpgsqlTransaction tran = conn.BeginTransaction();

                LargeObjectManager lbm = new LargeObjectManager(conn);
                int noid = lbm.Create(LargeObjectManager.READWRITE);
                LargeObject largeObject = lbm.Open(noid, LargeObjectManager.READWRITE);
                byte[] buffer = new byte[media.Length];
                media.Read(buffer, 0, (int)media.Length);
                BufferToLargeObject(buffer, largeObject, rpu, caller);
                largeObject.Close();

                int newId = 0;
                using (NpgsqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "INSERT INTO \"MediaContent\" (data, media_type) VALUES (:data, :type) RETURNING id;";
                    cmd.Parameters.Add("data", noid);
                    cmd.Parameters.Add("type", type.ToString());
                    newId = Convert.ToInt32(PostgreSQLConn.ExecuteScalar(cmd, Parent.CurrentUser));
                }
                tran.Commit();

                return newId;
            }
        }
        /// <summary>
        /// Checks if there is a media from the given type in the list
        /// </summary>
        /// <param name="mediaList">The media list.</param>
        /// <param name="mediaType">Type of the media.</param>
        /// <returns></returns>
        /// <remarks>Documented by Dev08, 2009-07-16</remarks>
        private bool ExistsMedia(IList<IMedia> mediaList, EMedia mediaType)
        {
            if (mediaList.Count == 0)
                return false;

            bool mediaFound = false;
            foreach (IMedia media in mediaList)
            {
                if (media.MediaType == mediaType)
                {
                    mediaFound = true;
                    break;
                }
            }

            return mediaFound;
        }