Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 177 lines (142 sloc) 6.431 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
// FirstRun.cs
//
// Copyright (c) 2008 Ethan Osten
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections;
using System.Threading;
using Gtk;

using Summa.Core;
using Summa.Core.Exceptions;
using Summa.Data;
using Summa.Gui;

namespace Summa.Gui {
    public class ProgressEventArgs : EventArgs {
        public double Progress;
        
        public ProgressEventArgs() {}
    }
    
    public class FirstRun : Window {
        private VBox vbox;
        private HBox hbox;
        private HButtonBox bbox;
        private Image image;
        private Table table;
        private Label label;
        private FileChooserDialog fcdialog;
        private FileChooserButton fcbutton;
        private Button cancel_button;
        private Button add_button;
        private ProgressBar pb;
        
        private ArrayList failed_feeds;
        
        public FirstRun() : base(WindowType.Toplevel) {
            IconName = Stock.Convert;
            Title = "Import OPML file";
            
            Resizable = false;
            BorderWidth = 6;
            
            vbox = new VBox(false, 6);
            Add(vbox);
            
            hbox = new HBox(false, 6);
            vbox.PackStart(hbox);
            
            image = new Image("dialog-question", IconSize.Dialog);
            hbox.PackStart(image);
            
            table = new Table(2, 3, false);
            table.RowSpacing = 6;
            hbox.PackStart(table);
            
            label = new Label("To import feeds, select an OPML file.");
            label.LineWrap = true;
            table.Attach(label, 1, 2, 0, 1);
            
            fcdialog = new OpmlDialog();
            fcbutton = new FileChooserButton(fcdialog);
            table.Attach(fcbutton, 1, 2, 1, 2);
            
            bbox = new HButtonBox();
            vbox.PackEnd(bbox);
            
            cancel_button = new Button(Stock.Cancel);
            cancel_button.Clicked += OnCancel;
            bbox.PackEnd(cancel_button);
            
            add_button = new Button(Stock.Convert);
            add_button.Clicked += new EventHandler(OnImport);
            bbox.PackEnd(add_button);
        }
        
        private void OnCancel(object obj, EventArgs args) {
            Destroy();
        }
        
        private void OnImport(object obj, EventArgs args) {
            Title = "Importing...";
            add_button.Sensitive = false;
            
            pb = new ProgressBar();
            table.Attach(pb, 1, 2, 1, 2);
            pb.Show();
            
            Thread thread = new Thread(ImportThread);
            thread.Start();
        }
        
        private void ImportThread() {
            failed_feeds = new ArrayList();
            
            if ( fcdialog.Uris.Length > 0 ) {
                string uri = fcdialog.Uri;
                ArrayList feeds = Feeds.ImportOpml(uri);
                double step = 1.0/feeds.Count;
                double progress = 0.0;
                
                foreach ( string feed in feeds ) {
                    NotificationEventArgs args = new NotificationEventArgs();
                    args.Message = "Importing feed \""+feed+"\"";
                    Gtk.Application.Invoke(this, args, new EventHandler(OnNotify));
                    
                    bool it_worked = true;
                    
                    try {
                        Feeds.RegisterFeed(feed);
                        it_worked = true;
                    } catch ( BadFeed e ) {
                        Log.Exception(e);
                        
                        args = new NotificationEventArgs();
                        args.Message = "Import of feed \""+feed+"\" failed";
                        Gtk.Application.Invoke(this, args, new EventHandler(OnNotify));
                        
                        failed_feeds.Add(feed);
                        
                        it_worked = false;
                    }
                    
                    ProgressEventArgs pargs = new ProgressEventArgs();
                    pargs.Progress = progress;
                    Gtk.Application.Invoke(this, pargs, new EventHandler(OnProgress));
                    
                    progress += step;
                    
                    if ( it_worked ) {
                        args = new NotificationEventArgs();
                        args.Message = "Import of feed \""+feed+"\" was successful";
                        Gtk.Application.Invoke(this, args, new EventHandler(OnNotify));
                    }
                }
            }
            Gtk.Application.Invoke(this, new EventArgs(), new EventHandler(OnCancel));
            Gtk.Application.Invoke(this, new EventArgs(), new EventHandler(OnDialog));
        }
        
        private void OnDialog(object obj, EventArgs args) {
            Window md = new MessageDialog(failed_feeds);
            md.ShowAll();
        }
        
        private void OnNotify(object obj, EventArgs args) {
            NotificationEventArgs iargs = (NotificationEventArgs)args;
            Notifier.Notify(iargs.Message);
        }
        
        private void OnProgress(object obj, EventArgs args) {
            ProgressEventArgs pargs = (ProgressEventArgs)args;
            pb.Fraction = pargs.Progress;
        }
    }
}
Something went wrong with that request. Please try again.