public static void Save(Book.Book book, BookServer bookServer, Color backColor, WebSocketProgress progress, AndroidPublishSettings settings = null) { var progressWithL10N = progress.WithL10NPrefix("PublishTab.Android.File.Progress."); var folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (!string.IsNullOrWhiteSpace(Settings.Default.BloomDeviceFileExportFolder) && Directory.Exists(Settings.Default.BloomDeviceFileExportFolder)) { folder = Settings.Default.BloomDeviceFileExportFolder; } var initialPath = Path.Combine(folder, book.Storage.FolderName + BookCompressor.BloomPubExtensionWithDot); var bloomdFileDescription = LocalizationManager.GetString("PublishTab.Android.bloomdFileFormatLabel", "Bloom Book for Devices", "This is shown in the 'Save' dialog when you save a bloom book in the format that works with the Bloom Reader Android App"); var filter = $"{bloomdFileDescription}|*{BookCompressor.BloomPubExtensionWithDot}"; var destFileName = Utils.MiscUtils.GetOutputFilePathOutsideCollectionFolder(initialPath, filter); if (String.IsNullOrEmpty(destFileName)) { return; } Settings.Default.BloomDeviceFileExportFolder = Path.GetDirectoryName(destFileName); PublishToAndroidApi.CheckBookLayout(book, progress); PublishToAndroidApi.SendBook(book, bookServer, destFileName, null, progressWithL10N, (publishedFileName, bookTitle) => progressWithL10N.GetMessageWithParams("Saving", "{0} is a file path", "Saving as {0}", destFileName), null, backColor, settings: settings); PublishToAndroidApi.ReportAnalytics("file", book); }
public static void Save(Book.Book book, BookServer bookServer, Color backColor, WebSocketProgress progress, AndroidPublishSettings settings = null) { var progressWithL10N = progress.WithL10NPrefix("PublishTab.Android.File.Progress."); using (var dlg = new DialogAdapters.SaveFileDialogAdapter()) { dlg.DefaultExt = BookCompressor.ExtensionForDeviceBloomBook; var bloomdFileDescription = LocalizationManager.GetString("PublishTab.Android.bloomdFileFormatLabel", "Bloom Book for Devices", "This is shown in the 'Save' dialog when you save a bloom book in the format that works with the Bloom Reader Android App"); dlg.Filter = $"{bloomdFileDescription}|*{BookCompressor.ExtensionForDeviceBloomBook}"; dlg.FileName = Path.GetFileName(book.FolderPath) + BookCompressor.ExtensionForDeviceBloomBook; if (!string.IsNullOrWhiteSpace(Settings.Default.BloomDeviceFileExportFolder) && Directory.Exists(Settings.Default.BloomDeviceFileExportFolder)) { dlg.InitialDirectory = Settings.Default.BloomDeviceFileExportFolder; //(otherwise leave to default save location) } dlg.OverwritePrompt = true; if (DialogResult.OK == dlg.ShowDialog()) { Settings.Default.BloomDeviceFileExportFolder = Path.GetDirectoryName(dlg.FileName); PublishToAndroidApi.CheckBookLayout(book, progress); PublishToAndroidApi.SendBook(book, bookServer, dlg.FileName, null, progressWithL10N, (publishedFileName, bookTitle) => progressWithL10N.GetMessageWithParams("Saving", "{0} is a file path", "Saving as {0}", dlg.FileName), null, backColor, settings: settings); PublishToAndroidApi.ReportAnalytics("file", book); } } }
/// <summary> /// Send the book to a client over local network, typically WiFi (at least on Android end). /// This is currently called on the UDPListener thread. /// Enhance: if we spin off another thread to do the transfer, especially if we create the file /// and read it into memory once and share the content, we can probably serve multiple /// requesting devices much faster. Currently, we are only handling one request at a time, /// since we pause advertising while sending and ignore requests that come in during sending. /// If the user switches away from the Android tab while a transfer /// is in progress, the thread will continue and complete the request. Quitting Bloom /// is likely to leave the transfer incomplete. /// </summary> /// <param name="book"></param> /// <param name="androidIpAddress"></param> /// <param name="androidName"></param> private void StartSendBookToClientOnLocalSubNet(Book.Book book, string androidIpAddress, string androidName, Color backColor) { // Locked in case more than one thread at a time can handle incoming packets, though I don't think // this is true. Also, Stop() on the main thread cares whether _wifiSender is null. lock (this) { // We only support one send at a time. If we somehow get more than one request, we ignore the other. // The device will retry soon if still listening and we are still advertising. if (_wifiSender != null) // indicates transfer in progress { return; } // now THIS transfer is 'in progress' as far as any thread checking this is concerned. _wifiSender = new WebClient(); } _wifiSender.UploadDataCompleted += (sender, args) => { // Runs on the async transfer thread AFTER the transfer initiated below. if (args.Error != null) { ReportException(args.Error); } // Should we report if canceled? Thinking not, we typically only cancel while shutting down, // it's probably too late for a useful report. // To avoid contention with Stop(), which may try to cancel the send if it finds // an existing wifiSender, and may destroy the advertiser we are trying to restart. lock (this) { Debug.WriteLine($"upload completed, sender is {_wifiSender}, cancelled is {args.Cancelled}"); if (_wifiSender != null) // should be null only in a desperate abort-the-thread situation. { _wifiSender.Dispose(); _wifiSender = null; } if (_wifiAdvertiser != null) { _wifiAdvertiser.Paused = false; } } }; // Now we actually start the send...but using an async API, so there's no long delay here. PublishToAndroidApi.SendBook(book, _bookServer, null, (publishedFileName, bloomDPath) => { var androidHttpAddress = "http://" + androidIpAddress + ":5914"; // must match BloomReader SyncServer._serverPort. _wifiSender.UploadDataAsync(new Uri(androidHttpAddress + "/putfile?path=" + Uri.EscapeDataString(publishedFileName)), File.ReadAllBytes(bloomDPath)); }, _progress, (publishedFileName, bookTitle) => _progress.GetMessageWithParams(id: "Sending", comment: "{0} is the name of the book, {1} is the name of the device", message: "Sending \"{0}\" to device {1}", parameters: new object[] { bookTitle, androidName }), null, backColor); PublishToAndroidApi.ReportAnalytics("wifi", book); }
/// <summary> /// Attempt to connect to a device /// </summary> /// <param name="book"></param> public void Connect(Book.Book book, Color backColor, AndroidPublishSettings settings = null) { try { // Calls to this come from JavaScript, not sure they will always be on the UI thread. // Before I added this, I definitely saw race conditions with more than one thread trying // to figure out what was connected. lock (this) { PublishToAndroidApi.CheckBookLayout(book, _progress); if (_connectionHandler != null) { // we're in an odd state...should only be able to click the button that calls this // while stopped. // Try to really get into the right state in case the user tries again. _androidDeviceUsbConnection.StopFindingDevice(); return; } // Create this while locked...once we have it, can't enter the main logic of this method // on another thread. _connectionHandler = new BackgroundWorker(); } _progress.Message(idSuffix: "LookingForDevice", comment: "This is a progress message; MTP is an acronym for the system that allows computers to access files on devices.", message: "Looking for an Android device connected by USB cable and set up for MTP..."); _androidDeviceUsbConnection.OneReadyDeviceFound = HandleFoundAReadyDevice; _androidDeviceUsbConnection.OneReadyDeviceNotFound = HandleFoundOneNonReadyDevice; // Don't suppress the first message after (re)starting. _previousDeviceNotFoundReportType = DeviceNotFoundReportType.Unknown; _connectionHandler.DoWork += (sender, args) => _androidDeviceUsbConnection.ConnectAndSendToOneDevice(book, backColor, settings); _connectionHandler.RunWorkerCompleted += (sender, args) => { if (args.Error != null) { UsbFailConnect(args.Error); } _connectionHandler = null; // now OK to try to connect again. }; _connectionHandler.RunWorkerAsync(); } catch (Exception e) { UsbFailConnect(e); } }
// internal virtual for testing only protected virtual void SendBookDoWork(Book.Book book, Color backColor) { PublishToAndroidApi.SendBook(book, _bookServer, null, (publishedFileName, path) => { _lastPublishedBloomdSize = GetSizeOfBloomdFile(path); _androidDeviceUsbConnection.SendBook(path); }, _progress, (publishedFileName, bookTitle) => _androidDeviceUsbConnection.BookExists(publishedFileName) ? _progress.GetTitleMessage("ReplacingBook", "Replacing existing \"{0}\"...", bookTitle) : _progress.GetTitleMessage("SendingBook", "Sending \"{0}\" to your Android device...", bookTitle), publishedFileName => _androidDeviceUsbConnection.BookExists(publishedFileName), backColor); PublishToAndroidApi.ReportAnalytics("usb", book); }
public PublishAudioVideoAPI(BloomWebSocketServer bloomWebSocketServer, PublishToAndroidApi publishToAndroidApi) { _webSocketServer = bloomWebSocketServer; _publishToAndroidApi = publishToAndroidApi; }
public void Start(Book.Book book, CollectionSettings collectionSettings, Color backColor, AndroidPublishSettings publishSettings = null) { if (_wifiAdvertiser != null) { Stop(); } // This listens for a BloomReader to request a book. // It requires a firewall hole allowing Bloom to receive messages on _portToListen. // We initialize it before starting the Advertiser to avoid any chance of a race condition // where a BloomReader manages to request an advertised book before we start the listener. _wifiListener = new BloomReaderUDPListener(); _wifiListener.NewMessageReceived += (sender, args) => { var json = Encoding.UTF8.GetString(args.Data); try { dynamic settings = JsonConvert.DeserializeObject(json); // The property names used here must match the ones in BloomReader, doInBackground method of SendMessage, // a private class of NewBookListenerService. var androidIpAddress = (string)settings.deviceAddress; var androidName = (string)settings.deviceName; // This prevents the device (or other devices) from queuing up requests while we're busy with this one. // In effect, the Android is only allowed to request a retry after we've given up this try at sending. // Of course, there are async effects from network latency. But if we do get another request while // handling this one, we will ignore it, since StartSendBook checks for a transfer in progress. _wifiAdvertiser.Paused = true; StartSendBookOverWiFi(book, androidIpAddress, androidName, backColor, publishSettings); // Returns immediately. But we don't resume advertisements until the async send completes. } // If there's something wrong with the JSON (maybe an obsolete or newer version of reader?) // just ignore the request. catch (Exception ex) when(ex is JsonReaderException || ex is JsonSerializationException) { _progress.Message(idSuffix: "BadBookRequest", message: "Got a book request we could not process. Possibly the device is running an incompatible version of BloomReader?", progressKind: ProgressKind.Error); //this is too technical/hard to translate _progress.MessageWithoutLocalizing($" Request contains {json}; trying to interpret as JSON we got {ex.Message}", kind: ProgressKind.Error); } }; var pathHtmlFile = book.GetPathHtmlFile(); _wifiAdvertiser = new WiFiAdvertiser(_progress) { BookTitle = BookStorage.SanitizeNameForFileSystem(book.Title), // must be the exact same name as the file we will send if requested TitleLanguage = book.BookData.Language1.Iso639Code, BookVersion = Book.Book.MakeVersionCode(File.ReadAllText(pathHtmlFile), pathHtmlFile) }; PublishToAndroidApi.CheckBookLayout(book, _progress); _wifiAdvertiser.Start(); var part1 = LocalizationManager.GetDynamicString(appId: "Bloom", id: "PublishTab.Android.Wifi.Progress.WifiInstructions1", englishText: "On the Android, run Bloom Reader, open the menu and choose 'Receive Books via WiFi'."); var part2 = LocalizationManager.GetDynamicString(appId: "Bloom", id: "PublishTab.Android.Wifi.Progress.WifiInstructions2", englishText: "You can do this on as many devices as you like. Make sure each device is connected to the same network as this computer."); // can only have one instruction up at a time, so we concatenate these _progress.MessageWithoutLocalizing(part1 + " " + part2, ProgressKind.Instruction); }
/// <summary> /// Send the book to a client over local network, typically WiFi (at least on Android end). /// This is currently called on the UDPListener thread. /// Enhance: if we spin off another thread to do the transfer, especially if we create the file /// and read it into memory once and share the content, we can probably serve multiple /// requesting devices much faster. Currently, we are only handling one request at a time, /// since we pause advertising while sending and ignore requests that come in during sending. /// If the user switches away from the Android tab while a transfer /// is in progress, the thread will continue and complete the request. Quitting Bloom /// is likely to leave the transfer incomplete. /// </summary> private void StartSendBookToClientOnLocalSubNet(Book.Book book, string androidIpAddress, string androidName, Color backColor, AndroidPublishSettings settings = null) { // Locked in case more than one thread at a time can handle incoming packets, though I don't think // this is true. Also, Stop() on the main thread cares whether _wifiSender is null. lock (this) { // We only support one send at a time. If we somehow get more than one request, we ignore the other. // The device will retry soon if still listening and we are still advertising. if (_wifiSender != null) // indicates transfer in progress { return; } // now THIS transfer is 'in progress' as far as any thread checking this is concerned. _wifiSender = new WebClient(); } _wifiSender.UploadDataCompleted += WifiSenderUploadCompleted; // Now we actually start the send...but using an async API, so there's no long delay here. PublishToAndroidApi.SendBook(book, _bookServer, null, (publishedFileName, bloomDPath) => { var androidHttpAddress = "http://" + androidIpAddress + ":5914"; // must match BloomReader SyncServer._serverPort. _wifiSender.UploadDataAsync(new Uri(androidHttpAddress + "/putfile?path=" + Uri.EscapeDataString(publishedFileName)), File.ReadAllBytes(bloomDPath)); Debug.WriteLine($"upload started to http://{androidIpAddress}:5914 ({androidName}) for {publishedFileName}"); }, _progress, (publishedFileName, bookTitle) => _progress.GetMessageWithParams(idSuffix: "Sending", comment: "{0} is the name of the book, {1} is the name of the device", message: "Sending \"{0}\" to device {1}", parameters: new object[] { bookTitle, androidName }), null, backColor, settings: settings); // Occasionally preparing a book for sending will, despite our best efforts, result in a different sha. // For example, it might change missing or out-of-date mp3 files. In case the sha we just computed // is different from the one we're advertising, update the advertisement, so at least subsequent // advertisements will conform to the version the device just got. _wifiAdvertiser.BookVersion = BookCompressor.LastVersionCode; lock (this) { // The UploadDataCompleted event handler quit working at Bloom 4.6.1238 Alpha (Windows test build). // The data upload still works, but the event handler is *NEVER* called. Trying to revise the upload // by using UploadDataTaskAsync with async/await did not work any better: the await never happened. // To get around this bug, we introduce a timer that periodically checks the IsBusy flag of the // _wifiSender object. It's a hack, but I haven't come up with anything better in two days of // looking at this problem. // See https://issues.bloomlibrary.org/youtrack/issue/BL-7227 for details. if (_uploadTimer == null) { _uploadTimer = new System.Timers.Timer { Interval = 500.0, Enabled = false }; _uploadTimer.Elapsed += (sender, args) => { if (_wifiSender != null && _wifiSender.IsBusy) { return; } _uploadTimer.Stop(); Debug.WriteLine("upload timed out, appears to be finished"); WifiSenderUploadCompleted(_uploadTimer, null); }; } _uploadTimer.Start(); } PublishToAndroidApi.ReportAnalytics("wifi", book); }
public delegate PublishView Factory(); //autofac uses this public PublishView(PublishModel model, SelectedTabChangedEvent selectedTabChangedEvent, LocalizationChangedEvent localizationChangedEvent, BookTransfer bookTransferrer, LoginDialog login, NavigationIsolator isolator, PublishToAndroidApi publishApi, PublishEpubApi publishEpubApi, BloomWebSocketServer webSocketServer) { _bookTransferrer = bookTransferrer; _loginDialog = login; _isolator = isolator; _publishApi = publishApi; _publishEpubApi = publishEpubApi; _webSocketServer = webSocketServer; InitializeComponent(); if (this.DesignMode) { return; } _model = model; _model.View = this; _makePdfBackgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(_makePdfBackgroundWorker_RunWorkerCompleted); _pdfViewer.PrintProgress += new System.EventHandler <PdfPrintProgressEventArgs>(OnPrintProgress); // BL-625: With mono, if a RadioButton group has its AutoCheck properties set to true, the default RadioButton.OnEnter // event checks to make sure one of the RadioButtons is checked. If none are checked, the one the mouse pointer // is over is checked, causing the CheckChanged event to fire. if (SIL.PlatformUtilities.Platform.IsMono) { SetAutoCheck(false); } //NB: just triggering off "VisibilityChanged" was unreliable. So now we trigger //off the tab itself changing, either to us or away from us. selectedTabChangedEvent.Subscribe(c => { if (c.To == this) { Activate(); } else if (c.To != this) { Deactivate(); } }); //TODO: find a way to call this just once, at the right time: // DeskAnalytics.Track("Publish"); //#if DEBUG // var linkLabel = new LinkLabel() {Text = "DEBUG"}; // linkLabel.Click+=new EventHandler((x,y)=>_model.DebugCurrentPDFLayout()); // tableLayoutPanel1.Controls.Add(linkLabel); //#endif _menusToolStrip.BackColor = _layoutChoices.BackColor = tableLayoutPanel1.BackColor = Palette.GeneralBackground; if (SIL.PlatformUtilities.Platform.IsMono) { BackgroundColorsForLinux(); } // Adding this renderer prevents a white line from showing up under the components. _menusToolStrip.Renderer = new EditingView.FixedToolStripRenderer(); // As far as I can tell, this is not needed anymore, and its presence, // at least in this place in the code, causes errors when running command-line tools // like UploadCommand which needs a PublishView but must not have something fully initialized. //GeckoPreferences.Default["pdfjs.disabled"] = false; SetupLocalization(); localizationChangedEvent.Subscribe(o => { SetupLocalization(); UpdateLayoutChoiceLabels(); UpdateSaveButton(); }); // Make this extra box available to show when wanted. _previewBox = new PictureBox(); _previewBox.Visible = false; Controls.Add(_previewBox); _previewBox.BringToFront(); }