Пример #1
0
        // 0 - successful
        // 1 - existed
        // 2 - tab not exist
        // 3 - Different onwer
        // 4 - plugin not exist
        // 5 - failed
        public int AddRSSItemWithPlugin(int tabid, int pluginID)
        {
            int result = 5;

            try
            {
                RSSDBDataContext data = new RSSDBDataContext();
                List <Tab>       tabs = (from tab in data.Tabs
                                         where tab.ID == tabid
                                         select tab).ToList();
                if (tabs.Count == 0)
                {
                    return(2);
                }

                Tab tabToAdd = tabs[0];
                if (tabToAdd.UserID != GetCurrentUserID())
                {
                    return(3);
                }

                List <RSSPlugin> plugins = (from plu in data.RSSPlugins
                                            where plu.ID == pluginID
                                            select plu).ToList();
                if (plugins.Count == 0)
                {
                    return(4);
                }

                RSSPlugin plugin = plugins[0];

                //Kiểm tra đã tồn tại plugin trong tab hay chưa
                for (int i = 0; i < tabToAdd.RSSItems.Count; i++)
                {
                    if (tabToAdd.RSSItems[i].PluginID == pluginID)
                    {
                        return(1);
                    }
                }
                //---------------------------------------------------------------------------

                string[] fileNames = Directory.GetFiles(Server.MapPath("~") + @"\bin", plugin.DLLName);
                foreach (string fileName in fileNames)
                {
                    Assembly asm   = Assembly.LoadFile(fileName);
                    Type[]   types = asm.GetTypes();
                    foreach (Type type in types)
                    {
                        if (type.GetInterface("IRSSPlugin") != null)
                        {
                            IRSSPlugin pluginObject = Activator.CreateInstance(type) as IRSSPlugin;

                            RSSItem newItem = new RSSItem();
                            newItem.Name        = pluginObject.GetRSSName();
                            newItem.Description = pluginObject.GetRSSDescription();
                            newItem.RSSLink     = pluginObject.GetRSSWebsiteLink();
                            newItem.TabID       = tabid;
                            newItem.PluginID    = plugin.ID;
                            data.RSSItems.InsertOnSubmit(newItem);
                            data.SubmitChanges();
                            result = 0;
                        }
                    }
                }
            }
            catch
            {
                result = 5;
            }
            finally
            {
            }
            return(result);
        }